【问题标题】:How to select options from multiple dropdown menus Selenium如何从多个下拉菜单中选择选项 Selenium
【发布时间】:2019-08-09 16:05:51
【问题描述】:

我正在尝试从多个站收集每月气象站数据,但无法选择数据间隔框来为每个气象站选择适当的“每月”选项。

使用 Selenium 的 Select 功能,我可以使用以下代码更改第一个气象站的选项:

driver = webdriver.Chrome('path_to_driver')
driver.implicitly_wait(5)

driver.get("http://climate.weather.gc.ca/historical_data/search_historic_data_stations_e.html?searchType=stnProx&timeframe=1&txtRadius=25&selCity=&optProxType=park&selPark=44%7C23%7C65%7C17%7CKejimkujik+National+Park&txtCentralLatDeg=&txtCentralLatMin=&txtCentralLatSec=&txtCentralLongDeg=&txtCentralLongMin=&txtCentralLongSec=&optLimit=yearRange&StartYear=1840&EndYear=2019&Year=2019&Month=8&Day=3&selRowPerPage=10")

select = Select(driver.find_element_by_tag_name('select'))

select.select_by_visible_text("Monthly")

但是它只改变了第一个选择元素。

我还在其他站的 div 和 select 元素上尝试了.click()method,但都返回“元素不可交互”的错误。

我也尝试过使用动作链

driver = webdriver.Chrome('path_to_driver')
driver.implicitly_wait(5)

driver.get('http://climate.weather.gc.ca/historical_data/search_historic_data_stations_e.html?searchType=stnProx&timeframe=1&txtRadius=25&selCity=&optProxType=park&selPark=44%7C23%7C65%7C17%7CKejimkujik+National+Park&txtCentralLatDeg=&txtCentralLatMin=&txtCentralLatSec=&txtCentralLongDeg=&txtCentralLongMin=&txtCentralLongSec=&optLimit=yearRange&StartYear=1840&EndYear=2019&Year=2019&Month=8&Day=3&selRowPerPage=10')

# path to div element
kejipark_div_menu = driver.find_element_by_css_selector("#timeframe1-sm")
# path to select element
kejipark_select_submenu = driver.find_element_by_css_selector("#timeframe1-sm > option:nth-child(2)")


try:
    actions = ActionChains(driver)
    actions.move_to_element(kejipark_div_menu)
    actions.click(kejipark_hidden_submenu)
    actions.perform()

finally:
    driver.quit()

返回错误:“javascript 错误:无法读取未定义的属性 'left'”

我不熟悉 javascript,但我怀疑这可能是与正确选项元素交互的关键。有谁知道如何从多个下拉菜单中选择一个选项?

【问题讨论】:

  • 您需要发布 Selenium 尝试与之交互的全部或部分 HTML。

标签: python selenium parsing webdriver


【解决方案1】:

试试这个方法。您应该能够遍历每个站点并选择所需的时间间隔来收集数据。

#Find Each Station and loop through

Stations = driver.find_elements_by_xpath("//*[@class='row']/form/div[@class='col-lg-3 col-md-3 col-sm-3 col-xs-3']")
print("Number of Stations", len(Stations))
time.sleep(3)

for Station in Stations:
    print("The station name is: ", Station.text)
    # Find Data Interval and select Monthly
    Interval = driver.find_element_by_xpath("//div[text()='"+Station.text+"']/following-sibling::div//label[contains(text(),'Data Interval')]/following::select[@name='timeframe'][1]")
    Interval.click()
    Interval.send_keys("Monthly")
    time.sleep(2)

【讨论】:

    【解决方案2】:

    我可以使用 .click() 方法在没有 javascript 的情况下选择每月。

    但是,我使用了不同的选择器: //div[text()='KEJIMKUJIK 1']/following-sibling::div/following-sibling::div//label[text()='Data Interval']/following-sibling::select

    当然,您可以用任何电台替换“KEJIMKUJIK 1”文本。

    让我知道它是否有效。否则我可以添加我的代码。

    【讨论】:

      猜你喜欢
      • 2019-02-04
      • 2018-02-17
      • 1970-01-01
      • 2017-03-23
      • 1970-01-01
      • 1970-01-01
      • 2020-05-14
      • 2020-09-17
      • 1970-01-01
      相关资源
      最近更新 更多