kdani
kdani

Reputation: 847

Python + Selenium Dropdown issue

I have the following code

select = row.find_element_by_css_selector("select")
select.click()
opts = select.find_elements_by_css_selector("option")[1:]
Select(select).select_by_visible_text(choice(opts).text)

Actually, the form field drops down, and a random element flashes. So it seems working. But! It doesn't select the element. No JavaScript events are triggered, and the form field, or at least the rendered form field remains unchanged.

Upvotes: 0

Views: 711

Answers (2)

kdani
kdani

Reputation: 847

I figured out the problem: it it impossible to select an option from a dropped-down field. So, removing the fist click() solves the problem. I'm not sure that this behaves the same in each browser, but in Firefox, you can't do this.

This is not handled very well in Selenium, because it doesn't raise any exceptions.

Upvotes: 1

olan
olan

Reputation: 3648

You need to pass the select HTML element to the Select object:

select = Select(row.find_element_by_css_selector("select"))
select.select_by_index(1)

The Select has a number of different (and convenient) ways to choose what you are trying to "select": http://selenium.googlecode.com/git/docs/api/py/webdriver_support/selenium.webdriver.support.select.html

FYI, I'm not sure what your choice() is doing, but by index may be less error prone (and more robust). Also the click() is unnecessary.

Upvotes: 2

Related Questions