一、select下拉框基础知识:
""" "一、下拉列表分类" 1、select下拉列表 https://www.w3school.com.cn/tiy/t.asp?f=eg_html_elements_select Select类 from selenium.webdriver.support.select import Select 只处理select下拉列表框。 选值的3种方式: select_by_value option的value属性 select_by_index option所处的下标,从0开始的 select_by_visible_text option的文本内容 第一步: driver.find_element找到select元素对象 第二步: 实例化Select类,把第一步的对象传进去。 s = Select(第一步的对象) 第三步: 选值 select_by_value option的value属性 select_by_index option所处的下标,从0开始的 select_by_visible_text option的文本内容 2、div+p标签 百度-高级搜索 """
二、select下拉框代码:
from time import sleep from selenium import webdriver from selenium.webdriver.support.select import Select from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By # 打开一个浏览器 driver = webdriver.Chrome() #调整为最大窗口: driver.maximize_window() #打开一个窗口 driver.get("https://www.w3school.com.cn/tiy/t.asp?f=eg_html_elements_select") #切换到一个iframe driver.switch_to.frame("iframeResult") #1、找到select元素 selector_ele = driver.find_element(By.NAME,"cars") # 2、实例化Select类对象 s = Select(selector_ele) #3.选值 s.select_by_index(1) sleep(3) s.select_by_value("fiat") sleep(3) s.select_by_visible_text("Audi") #关闭弹窗 driver.quit()
三、非select下拉框
from time import sleep from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By # 打开一个浏览器 driver = webdriver.Chrome() #调整为最大窗口: driver.maximize_window() #打开一个窗口 driver.get("https://www.baidu.com/") element=driver.find_element(By.XPATH,'//*[@id="s-usersetting-top"]') element.click() sleep(3) #等待元素可见 loc = (By.XPATH,'//span[text()="高级搜索"]') ele = WebDriverWait(driver,10).until(EC.visibility_of_element_located(loc)) ele.click()
标签:web,webdriver,selenium,driver,Select,自动化,import,下拉框,select From: https://www.cnblogs.com/crdhm12040605/p/17209045.html