Reputation: 183
I have the following HTML code
<select name="countries" class_id="countries">
<option value="-1">--SELECT COUNTRY--</option>
<option value="459">New Zealand</option>
<option value="100">USA</option>
<option value="300">UK</option>
</select>
I am trying to get a list of the option values (like 459, 100, etc, not the text) using Selenium.
At the moment I have the following Python code
from selenium import webdriver
def country_values(website_url):
browser = webdriver.Firefox()
browser.get(website_url)
html_code=browser.find_elements_by_xpath("//select[@name='countries']")[0].get_attribute("innerHTML")
return html_code
As you can see the code returns pure HTML, which I am parsing with HTMLParser library. Is there any way to get the option values just using Selenium? In other words, without having to parse the result from Selenium?
Upvotes: 18
Views: 43269
Reputation: 51
Since selenuim methods like find_elements_by_tag_name
are now descripted,
TehTris' answer (which is great) might need a little update.
Firstly, we make sure we also import:
from selenium.webdriver.support.select import Select
Then we set our driver (aka browser) and go to the desired url:
driver = webdriver.Chrome(options=options)
driver.get("desired_url.com")
Now we create the select_box like this:
select_box = Select(driver.find_element(By.XPATH, '//select[@name="countries"]'))
And now we can extract the values by iterating over select_box.options
[i.get_attribute('value') for i in select_box.options]
Upvotes: 0
Reputation: 3477
Simpler version:
from selenium.webdriver.support.ui import Select
dropdown_menu = Select(driver.find_element_by_name(<NAME>))
for option in dropdown_menu.options:
print(option.text, option.get_attribute('value'))
Upvotes: 12
Reputation: 3217
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
Upvotes: 33
Reputation: 4212
A little improved answer. In Selenium and Python, if you want to get all attribute values, use following list comprehension:
options = [x.get_attribute("value") for x in driver.find_element_by_id("yourlocator").find_elements_by_tag_name("option")]
print(options)
Simple call to .text
won't work for lists.
Use:
options = [x.get_attribute("innerText") for x in driver.find_element_by_id("yourlocator").find_elements_by_tag_name("option")]
print(options)
Upvotes: 1
Reputation: 880777
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.
Upvotes: 15