1

I am trying to open a drop down and select a option but every time I use the find_element_by_id feature I get an error.

selenium.common.exceptions.ElementNotInteractableException: Message: Element <button id="dropDown" class="btn btn-default dropdown-toggle custom-button" name="dropDown" type="button"> could not be scrolled into view

Here is the dropdown and code

https://ibb.co/GtdLhYn

Some one had a similar problem and solved it but for some reason he didnt include how.

Message: Element <option> could not be scrolled into view while trying to click on an option within a dropdown menu through Selenium

He only said he used actionsChains but that didnt really work either

Eggnog
  • 33
  • 2
  • 11

1 Answers1

2

There are 3 ways to handle this issue.

1St Approach: location_once_scrolled_into_view

element = driver.find_element_by_id("dropDown")
# scroll to the element
element.location_once_scrolled_into_view
element.click()

2nd Approach: JavaScript (the button will be clicked though not scrolled into view.)

element = driver.find_element_by_id("dropDown")
driver.execute_script("arguments[0].click();",element)

3rd Approach: Clicking on the list item (link in your case directly)

element = driver.find_element_by_xpath("//a[@id='vehicleUsage_1' and contains(.,'Pleasure')]")
element.click()
supputuri
  • 13,644
  • 2
  • 21
  • 39
  • Im getting the same error on the first approach. As for the second one it runs but nothing happens. Is the drop down supposed to also open on screen when those JavaScript commands are run. – Eggnog Apr 03 '19 at 22:36
  • I see the ul with li, why don't you click li directly. And can you check what is the associated event to that button. – supputuri Apr 03 '19 at 22:43
  • Something like this `//div[@class='col-xs-12 dropup']//ul[@id='dropdownId']/li[normalize-space(.)='list_Item']`. you have to use javascrip to click on the li directly. – supputuri Apr 03 '19 at 22:47
  • Im sorry i am not familiar with JavaScript. When I try to find that element it like this driver.find_element_by_xpath("//div[@class='col-xs-12 dropup']//ul[@id='dropdownId']/li[normalize-space(.)='list_Item']").click(). I get a Faield to convert data to an object error – Eggnog Apr 03 '19 at 23:05
  • can you please share the first list item with html, so that I can provide the javascript to click on the first item,then you can change it accordingly. – supputuri Apr 03 '19 at 23:55
  • Ok, I would first like to check if we can directly interact with the links by `print(driver.find_element_by_xpath("//a[@id='vehicleUsage_1' and contains(.,'Pleasure')]").text)`. If `.text` does not return the value then try `.get_attribute('innerText')` – supputuri Apr 04 '19 at 00:53
  • If you get the text, then you can directly select that item by clicking on the link using javascript. – supputuri Apr 04 '19 at 00:55
  • Ohhh Dude your a genius. Thank You. – Eggnog Apr 04 '19 at 01:11
  • Thanks, and Glad that it resolved your issue. I would always try to select the list items using the xpath rather clicking on the list box and then clicking on the options. – supputuri Apr 04 '19 at 02:19