【问题标题】:Python Selenium Dropdown Menu SelectPython Selenium 下拉菜单选择
【发布时间】:2023-03-25 16:55:01
【问题描述】:

要单击此 page 上的“县”下拉菜单,我将此 XPath 与 Selenium 一起使用:

driver.find_elements_by_xpath('//div[@class="masterCustomDropDown"]/img')[3].click() 

由于有时它不会出错,也不会实际执行点击操作,因此我通常会检查下拉菜单中的元素是否可见,以查看点击功能是否执行。有没有更好的方法来做到这一点?

谢谢!

【问题讨论】:

    标签: python selenium selenium-webdriver iframe webdriverwait


    【解决方案1】:

    您可以尝试使用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)
    

    【讨论】:

      【解决方案2】:

      要点击下拉菜单,您需要:

      • 诱导 WebDriverWait 使所需的框架可用并切换到它
      • 诱导 WebDriverWait 使所需的元素可点击,您可以使用以下解决方案:
      • 代码块:

        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()
        
      • 浏览器快照:

      【讨论】:

      • 点击它没有问题。我在问是否有任何方法可以验证单击操作是否已执行?
      猜你喜欢
      • 2022-11-11
      • 2018-02-17
      • 2018-08-06
      • 2023-04-09
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 2022-07-08
      • 2023-01-19
      相关资源
      最近更新 更多