Listing select option values with Selenium and Python
import selenium.webdriver as webdriver
import selenium.webdriver.support.ui as UI
import contextlib
with contextlib.closing(webdriver.Firefox()) as driver:
driver.get(url)
select = UI.Select(driver.find_element_by_xpath('//select[@name="countries"]'))
for option in select.options:
print(option.text, option.get_attribute('value'))
prints
(u'--SELECT COUNTRY--', u'-1')
(u'New Zealand', u'459')
(u'USA', u'100')
(u'UK', u'300')
I learned this here. See also the docs.
check it out, here is how i did it before i knew what the Select Module did
from selenium import webdriver
browser = webdriver.Firefox()
#code to get you to the page
select_box = browser.find_element_by_name("countries")
# if your select_box has a name.. why use xpath?.....
# this step could use either xpath or name, but name is sooo much easier.
options = [x for x in select_box.find_elements_by_tag_name("option")]
# this part is cool, because it searches the elements contained inside of select_box
# and then adds them to the list options if they have the tag name "options"
for element in options:
print(element.get_attribute("value"))
# or append to list or whatever you want here
outputs like this
-1
459
100
300