culebrón
culebrón

Reputation: 36513

Dropdowns work only after two clicks in Selenium web driver

I am using Selenium with Python on the Browserstack platform.

I am testing a select2 dropdown, and I always need to click the dropdown twice to make it work:

dropdown_click = driver.find_element_by_css_selector('#s2id_autogen2 a.select2-choice')
actions.click(dropdown_click).perform()
actions.click(dropdown_click).perform()  # another click to open dropdown.
driver.implicitly_wait(2)
make_list = driver.find_element_by_id('select2-drop')
assert make_list.is_displayed()

There's implicitly_wait, but without the second click(), the dropdown is still invisible and assertion fails.

This is not the only JS dropdown that misbehaves this way for me in Selenium.

This JavaScript code works correctly in browsers, but in Selenium it's correct only when clicked the first time. The second time I have to call click() twice.

$('span.opener').on('click', function() {
    $(this).next('.dropdown').toggleClass('dropdown-visible');
})

What's wrong with Selenium and these dropdowns?

edit: explicit waits did fix this problem.

Upvotes: 1

Views: 577

Answers (2)

user2241537
user2241537

Reputation:

If it behaves like that using the statements below can work:

new SelectElement(driver.FindElement(By.Id("YourDropdownID"))).SelectByIndex(indexValueYouWantToSelct));

Or you can do this

new SelectElement(driver.FindElement(By.Id("YourDropdownID"))).SelectByText("YourDropDownItemText");

Or you can also try this

new SelectElement(driver.FindElement(By.Id("YourDropdownID"))).SelectByValue("YourDoropDownItemValue");

Upvotes: 0

Raymond
Raymond

Reputation: 614

You might want to hover to the dropdown first, then do your click operation. This helps me some times in the past.

Upvotes: 1

Related Questions