Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I want to select an option from a select field using Selenium & Python.

The HTML is as follows:

<select autocomplete="off" class="style_input_item" name="AccountEnable" id="Enable" value="0" onchange="onPageDataChange()">
    <option value="0" selected="selected"><script>T("Disabled")</script>Disabled</option>
    <option value="1"><script>T("Enabled")</script>Enabled</option>
</select>

And I tried as follows:

driver.find_element_by_xpath('//*[@id="Enable"]/option[value="1"]').click()

I received that error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="Enable"]/option[value="0"]"}

share|improve this question
    
Possible duplicate of Selenium - Python - drop-down menu option value. The accepted answer isn't the best way.The best way is alecxe's answer. See also sqa.stackexchange.com/questions/1355/…. – JeffC 19 hours ago
    
The accepted answer isn't the best way.The best way is alecxe's answer. See also sqa.stackexchange.com/questions/1355/…. – JeffC 19 hours ago

Just try:

mydriver.find_element_by_xpath('//*[@id="Enable"]/option[@value="1"]').click()

or

mydriver.find_element_by_xpath('//*[@id="Enable"]/option[2]').click()
share|improve this answer

Make sure you include Select:

from selenium.webdriver.support.select import Select

then

select = Select(driver.find_element_by_id('Enable'))
select.select_by_index(0)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.