-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?

2

1 Answer 1

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')
Sign up to request clarification or add additional context in comments.

7 Comments

You should be using Select... it's built specifically to handle SELECT elements and OPTIONS.
@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.
He's using it wrong. All of the select_by_*s return void... so what is it supposed to print???
The .click() after select_by_value() should be removed. It's not needed since select_by_value() actually selects the value.
@JeffC - Well, OP did use Select(selectbox).select_by_value('0') but it didn't work for him.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.