[英]Python Selenium Dropdown Menu Select
要單擊此頁面上的“縣”下拉菜單,我將此XPath與Selenium結合使用:
driver.find_elements_by_xpath('//div[@class="masterCustomDropDown"]/img')[3].click()
由於有時它不會出錯,也不會實際執行單擊操作,因此我通常檢查下拉菜單中的元素是否可見,以查看是否執行了單擊功能。 有一個更好的方法嗎?
謝謝!
您可以嘗試使用Select()
//Create a new select element
Select choose = new Select(driver.find_elements_by_xpath('//div[@class="masterCustomDropDown"]/img'))
//Select the element at the 3rd index in the Select element we have reference to
choose.selectByIndex(3)
要點擊縣下拉菜單,您需要:
代碼塊:
from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC options = Options() options.add_argument("start-maximized") options.add_argument("disable-infobars") options.add_argument("--disable-extensions") driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\\Utility\\BrowserDrivers\\chromedriver.exe') driver.get("https://reports.blm.gov/report/LR2000/23/Pub-MC-Geo-Index") WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"dispReport"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[@title='County']//following::img[1]"))).click()
瀏覽器快照:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.