非select/option元素:web
1.觸發下拉列表出現ui
2.等待下拉列表中的元素出現,而後進行選擇元素便可。spa
下拉框操做-Select類
selenium提供Select類來處理select/optioncode
from selenium.webdriver.support.ui import Selectorm
ele = driver.find_element_by_xpath(元素定位表達式)
s = Select(ele)對象
s.select_by_value(value值) #經過value
s.select_by_index(index) #經過下標
s.select_by_visible_text(文本') #經過文本blog
from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains as AC from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By import time from selenium.webdriver.support.ui import Select driver = webdriver.Chrome() driver.get('https://www.baidu.com/') time.sleep(3) # 百度首頁,設置連接元素定位 ele = driver.find_element(By.XPATH,"//div[@id='u1']/a[text()='設置']") # 對設置連接進行鼠標懸浮操做 AC(driver).move_to_element(ele).perform() # 設置下拉框中選擇【高級搜索】,浮窗下拉列表可用click進行點擊操做 WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,"//a[text()='高級搜索']"))) driver.find_element_by_xpath("//a[text()='高級搜索']").click() # 等待高級設置搜索頁面可見 WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,'//select[@name="ft"]'))) # 建立Select對象,參數爲元素 ele = driver.find_element_by_xpath('//select[@name="ft"]') s = Select(ele) s.select_by_value('xls') #經過value time.sleep(2) s.select_by_index(1) #經過下標 time.sleep(2) s.select_by_visible_text('RTF 文件 (.rtf)') #經過文本
補充!!element
如何定位懸浮下拉列表get