-1

My task is to ckoose one of dropdown elements.

My HTML is:

<td>
<select name="subtract">
<option selected="selected" value="1">Да</option>
<option value="0">Нет</option>
</select>
</td>

My code is:

>>>selectbox = ff.find_element_by_name("subtract")
>>>print (selectbox.text)
Да
Нет
>>>print(Select(selectbox).options)
[<selenium.webdriver.remote.webelement.WebElement object at 0x00000000037992E8>, <selenium.webdriver.remote.webelement.WebElement object at 0x0000000003799278>]
>>>print(Select(selectbox).select_by_index(0))
None
>>>print(Select(selectbox).select_by_value('0'))
None
>>>print(Select(selectbox).select_by_visible_text('Нет'))
None

So I really can't find where I am wrong?

GhostKU
  • 1,898
  • 6
  • 23
  • 32
  • 1.click(wait for the drop down to open ) 2.scroll to the element 3. click on the element – Ran Adler Nov 04 '15 at 09:55
  • Possible duplicate of [Selecting a value from a drop-down option using selenium python](http://stackoverflow.com/questions/22524621/selecting-a-value-from-a-drop-down-option-using-selenium-python) – JeffC Nov 07 '15 at 20:32

1 Answers1

0

I'm not sure about Select. But try this code:

find_element_by_xpath("//select[@name='subtract']/option[@value='0']").click()

or this

find_element_by_xpath("//select[@name='subtract']/option[@value='1']").click()

EDIT: or try using Select using xpath:

Select(driver.find_element_by_xpath("//select[@name='subtract']").select_by_value('0')
JRodDynamite
  • 12,325
  • 5
  • 43
  • 63
  • You should be using Select... it's built specifically to handle `SELECT` elements and `OPTIONS`. – JeffC Nov 07 '15 at 20:31
  • @JeffC - Well, if you look at the question, OP did try to use `Select` but it didn't work. The code which OP used seems legit but it was still not selecting that element. Hence, I suggested to find the element and click on it. Anyways, updated the answer. Have a look. – JRodDynamite Nov 08 '15 at 05:08
  • He's using it wrong. All of the `select_by_*`s return void... so what is it supposed to print??? – JeffC Nov 08 '15 at 16:45
  • The `.click()` after `select_by_value()` should be removed. It's not needed since `select_by_value()` actually selects the value. – JeffC Nov 08 '15 at 16:47
  • @JeffC - Well, OP did use `Select(selectbox).select_by_value('0')` but it didn't work for him. – JRodDynamite Nov 08 '15 at 16:49
  • @JeffC - Correction made to the answer. Thanks! :) – JRodDynamite Nov 08 '15 at 16:49
  • Yep but he used `print(Select(selectbox).select_by_value('0'))` which returned `None`... but this statement returns `void` so I don't know whether he understood what he was doing and realized that it selected the option but printed `None` anyway. Hard to know at this point... – JeffC Nov 08 '15 at 17:07