【问题标题】:selenium drop down options and usgs webportalselenium 下拉选项和 usgs 门户网站
【发布时间】:2015-02-20 03:27:49
【问题描述】:

我按照下面的指南,点击区域下拉菜单下的非洲西部使用...

Selenium - Python - drop-down menu option value

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select

driver = webdriver.Firefox()
driver.get(usgs_dataportal_ppt)

driver.find_element_by_xpath("//select[@id='regionCombobox']    /option[text()='af-w']").click()

我还尝试发送查看除 regioncombobox 以外的其他容器的密钥,以查看是否可以将值更改为非洲西部。

网站是http://earlywarning.usgs.gov/adds/downloads/index.php

但是我不断收到 selenium 找不到元素的错误。

 selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"//select[@id='regionCombobox']/option[text()='af-w']"}

【问题讨论】:

    标签: php jquery python selenium drop-down-menu


    【解决方案1】:

    单击下拉按钮和wait 以显示带有Africa - West 文本的链接:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    driver = webdriver.Firefox()
    driver.get('http://earlywarning.usgs.gov/adds/downloads/index.php')
    
    # explicitly wait for button to appear
    button = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span.custom-combobox > a")))
    button.click()
    
    # explicitly wait for "Africa - West" link to appear
    africa_west = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.LINK_TEXT, "Africa - West")))
    africa_west.click()
    

    您可以通过以下方式获取并单击所有下拉按钮:

    WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span.custom-combobox > a")))
    
    buttons = driver.find_elements_by_css_selector('span.custom-combobox > a')
    
    # region
    buttons[0].click()
    
    # product
    buttons[1].click()
    
    # period
    buttons[2].click()   
    

    【讨论】:

    • 当我这样做时,我得到了 selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"css selector","selector":"span.custom-combobox >一个"}
    • @BaconDoggie 我认为我们也应该等待按钮出现 - 更新代码,检查一下。
    • 我是 selenium 的新手,只是想问你为什么我需要使用 css 选择器自定义框而不是组合框,然后等待 10 秒?也非常感谢你,你帮助了我。
    • @BaconDoggie 欢迎您。问题是您不能在此处使用Select 类并操纵select->option 构造,因为实际的select 元素是不可见的。您在屏幕上看到的下拉菜单实际上是一个 span 元素,右侧有一个作为“V”按钮的 a 元素。希望这可以解决问题。
    • 感谢您向我解释,这有点道理。我将对其他按钮应用相同的过程,以便自动下载这些文件。
    猜你喜欢
    • 1970-01-01
    • 2018-04-24
    • 2012-08-20
    • 2019-11-11
    • 1970-01-01
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    • 2021-04-02
    相关资源
    最近更新 更多