import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.select import Select # 导入选择服务包 driver = webdriver.Edge() driver.get("file:///D:\测试\WEB自动化\课件\测试\本地练习网页.html")
# 这里用的是本地网页,需要file:///开头 # 创建一个select对象 select_element = driver.find_element(By.ID, "s1Id") select_object = Select(select_element) time.sleep(3) # 有三种方式可以选择选项 # 通过选项索引 select_object.select_by_index(2) # 通过选项value属性值 # select_object.select_by_value('o3') # 通过选项文本 # select_object.select_by_visible_text('o1') # 检查所有被选择的选项--页面看不到效果,在代码中看 print('---------选中的所有选项-----------') all_select_options = select_object.all_selected_options print(all_select_options) # 返回列表中第一个被选择的选项 print('---------第一个被选中的选项-----------') first_select_options = select_object.first_selected_option print(first_select_options) # 返回列表的所有选项 print('---------列表所有选项-----------') all_available_options = select_object.options print(all_available_options)
标签:选项,WEB,object,列表,单选,print,import,options,select From: https://www.cnblogs.com/gezirui/p/17573781.html