Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select a dropdownlist value using webdriver in python

The html source is below

<select id="ca_vdcs" class="pulldown small" name="vdc" style="display: none;">
<option>-- Select a vDC --</option>
<option>Platform-VDC-org</option>
</select>

I want to select the 'Platform-VDC-org', but the below code is not working.

select = browser.find_element_by_id('ca_vdcs')
select.find_element_by_xpath("//option[@value='Platform-VDC-org']").click()
like image 769
Young Avatar asked Oct 29 '13 02:10

Young


People also ask

How a drop down value is selected using Webdriver?

The 'Select' class in Selenium WebDriver is used for selecting and deselecting option in a dropdown. The objects of Select type can be initialized by passing the dropdown webElement as parameter to its constructor.

How do I select a specific value from a dropdown in Selenium Webdriver?

New Selenium IDE A dropdown is represented by <select> tag and the options are represented by <option> tag. To select an option with its value we have to use the selectByValue method and pass the value attribute of the option that we want to select as a parameter to that method.


1 Answers

You should try using the Select() class. It makes dealing with select elements much easier.

select = Select(browser.find_element_by_id("ca_vdcs"))
select.select_by_visible_text("Platform-VDC-org")

You can see the WebDriver API bindings in Python here:

http://selenium-python.readthedocs.org/en/latest/api.html

The Select() class is at section 7.12. UI Support

like image 186
Mark Rowlands Avatar answered Sep 22 '22 21:09

Mark Rowlands