0

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"]"}

SkorpEN
  • 2,491
  • 1
  • 22
  • 28
bar barbar
  • 9
  • 1
  • 2
  • Possible duplicate of [Selenium - Python - drop-down menu option value](http://stackoverflow.com/questions/7867537/selenium-python-drop-down-menu-option-value). The accepted answer isn't the best way.The best way is alecxe's answer. See also http://sqa.stackexchange.com/questions/1355/what-is-the-correct-way-to-select-an-option-using-seleniums-python-webdriver. – JeffC Dec 06 '16 at 19:09
  • The accepted answer isn't the best way.The best way is alecxe's answer. See also http://sqa.stackexchange.com/questions/1355/what-is-the-correct-way-to-select-an-option-using-seleniums-python-webdriver. – JeffC Dec 06 '16 at 19:10

2 Answers2

0

Just try:

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

or

mydriver.find_element_by_xpath('//*[@id="Enable"]/option[2]').click()
Gerard Rozsavolgyi
  • 4,834
  • 4
  • 32
  • 39
-1

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)
Lucas Tierney
  • 2,503
  • 15
  • 13