简体   繁体   中英

How to select options from multiple dropdown menus Selenium

I am trying to collect Monthly weather station data from multiple stations but cannot select the Data Interval box to select the appropriate "Monthly" option for each weather station.

Using Selenium's Select function, I am able to change the first weather station's options using this code:

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")

However it only changes the first select element.

I have also tried .click() method on both the div and select elements for the other stations, but both return an error of "element not interactable".

I have also tried using action chains

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()

which returns the error: "javascript error: Cannot read property 'left' of undefined"

I am unfamiliar with javascript but I suspect that might be the key to interacting with the proper option elements. Does anyone know how to select a single option from multiple dropdown menus?

I was able to select monthly without javascript using just the .click() method.

However, I used a different selector: //div[text()='KEJIMKUJIK 1']/following-sibling::div/following-sibling::div//label[text()='Data Interval']/following-sibling::select

You can replace the 'KEJIMKUJIK 1' text with any station of course.

Let me know if it works. Otherwise I can add my code.

Try this approach. you should be able to loop through each station & select the desired interval to collect the data.

#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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM