1

I was following the guide below, to click on africa west under the regions drop down menu using...

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

I have also tried sending keys looking at different containers other than regioncombobox to see if i can change the values to africa west.

the website is http://earlywarning.usgs.gov/adds/downloads/index.php

However I keep getting an error that selenium cannot find the element.

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

1 Answers1

1

Click on the dropdown button and wait for the link with Africa - West text to appear:

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

Here is how you can get and click all the dropdown buttons:

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()   
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • When I did that I got selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"css selector","selector":"span.custom-combobox > a"} – BaconDoggie Feb 19 '15 at 17:44
  • @BaconDoggie I think we should wait for button to appear also - updated the code, check it out. – alecxe Feb 19 '15 at 17:50
  • I am new to selenium and just wanted to ask you how come i needed to use css selector custom box instead of the combobox and then wait 10 seconds? Also Thank you so much, you helped me out. – BaconDoggie Feb 19 '15 at 17:56
  • @BaconDoggie you are welcome. The problem is that you cannot use `Select` class here and manipulate select->option construction because an actual `select` element is invisible. The dropdown you see on the screen is actually a span element with an a element as a "V" button on the right. Hope this clears things up. – alecxe Feb 19 '15 at 18:00
  • Thanks for explaining it to me, it sorta makes sense. I will apply the same process to the other buttons so I can auto download these files. – BaconDoggie Feb 19 '15 at 18:05
  • If I could bother you with one more question, it looks like whenever i use the code you gave me above i notice that it clicks through all the options that have custom-combobox as the class when you first run it. I wanted to know how I could distinguish between each combobox so that I click on the other buttons (which are also custom comboboxes) – BaconDoggie Feb 19 '15 at 18:35
  • @BaconDoggie there are different ways to do it. The simplest is to refer to each button by index - first is region, second is product, third is period. – alecxe Feb 19 '15 at 18:37
  • this will sound dumb but my first insinct was to try button[0].click(), but i guess that was not it – BaconDoggie Feb 19 '15 at 18:44
  • @BaconDoggie I've updated an answer and provided a sample code to click dropdown buttons, give it a try. – alecxe Feb 19 '15 at 19:29