我按照下面的指南,使用...点击区域下拉菜单下的非洲西部。
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.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"//select[@id='regionCombobox']/option[text()='af-w']"}
答案 0 :(得分: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()