Message: Element option could not be scrolled into view while trying to click on an option within a dropdown menu through Selenium
This error message...
selenium.common.exceptions.ElementNotInteractableException: Message: Element <option> could not be scrolled into view.
...implies that the <option>
item which your program was trying to interact with could not be scrolled into view.
The HTML of the desired element would have given us some idea behind the error. However it seems the desired element was not clickable
/ within the Viewport. To address the issue you have to induce WebDriverWait for the element to be clickable and you can use the following solution:
mySelectElement = browser.find_element_by_id('providerTypeDropDown')
dropDownMenu = Select(mySelectElement)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='providerTypeDropDown']//options[contains(.,'Professional')]")))
dropDownMenu.select_by_visible_text('Professional')
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
How to fix could not be scrolled into view problem with drop down menu?
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()
Selenium: Element span could not be scrolled into view
Based on reading your post, I think that you can use the following xpath
in your Chome Dev Tools ( F12 ) to determine how many children elements display
//div[@class='thread-node-children-show']
Based on this result ( 1 of _ ), you could use the scrollIntoView
method to navigate to the element using a for-loop
.
Example Code
numOfElements = driver.find_elements(By.XPATH, "//div[@class='thread-node-children-show']").__len__()
for num in range(numOfElements):
element = driver.find_element(By.XPATH, "//div[@class='thread-node-children-show'][{0}]".format(num + 1))
driver.execute_script("return arguments[0].scrollIntoView();", element)
Selenium python Error: element could not be scrolled into view
This error message...
selenium.common.exceptions.ElementNotInteractableException: Message: Element <span class="ui-button-text"> could not be scrolled into view
...implies that the WebDriver instance i.e. driver was unable to scroll the element within the Viewport to invoke click()
.
First of all, as your usecase is to invoke click()
on the element, ideally instead of using presence_of_element_located()
you need to use the ExpectedConditions as element_to_be_clickable()
as follows:
WebDriverWait(driver, 1000000).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[5]/div[3]/div/button/span'))).click()
You can find a couple of detailed discussions in:
- Message: Element could not be scrolled into view while trying to click on an option within a dropdown menu through Selenium
- org.openqa.selenium.ElementNotInteractableException: Element could not be scrolled into view when trying to click a button
As an alternative, as per the error message, to scroll an element within the Viewport before invoking click()
you can also use the Element.scrollIntoView() method.
You can find a detailed discussion in:
- What is the difference between the different scroll options?
At this point it is worth to mention, the following methods:
- move_to_element() from selenium.webdriver.common.action_chains
- element_to_be_clickable() from selenium.webdriver.support.expected_conditions
will automatically scroll the element within the Viewport.
You can find a detailed discussion in:
- How to scroll a webpage using selenium webdriver in Python without using javascript method execute_script()
This usecase
The button with text as Continue is within the Top Level Content but rendered within a Modal Dialog Box.
DevTools Snapshot:
As the desired element is within a Modal Dialog Box, so to locate and invoke click()
on the element you have to induce WebDriverWait for the element_to_be_clickable()
and you can use the following Locator Strategy:
Using
XPATH
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(@aria-describedby, 'ui-id-')]//span[@class='ui-button-text' and text()='Continue']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
DevTools Snapshot:
Related Topics
Force Python to Forego Native SQLite3 and Use the (Installed) Latest SQLite3 Version
Connecting Slots and Signals in Pyqt4 in a Loop
Display a 'Loading' Message While a Time Consuming Function Is Executed in Flask
Plotting 3-Tuple Data Points in a Surface/Contour Plot Using Matplotlib
Python VS Groovy VS Ruby? (Based on Criteria Listed in Question)
Simple File Server to Serve Current Directory
Vscode -- How to Set Working Directory for Debugging a Python Program
How to Operate on a Dataframe with a Series for Every Column
Creating a List of Dictionaries Results in a List of Copies of the Same Dictionary
Why Is "If Not Someobj:" Better Than "If Someobj == None:" in Python
Running Windows Shell Commands with Python
Replace Characters Not Working in Python
Passing a Matplotlib Figure to HTML (Flask)
R Markdown: How to Make Rstudio Display Python Plots Inline Instead of in New Window
Ruby Equivalent of Python's "Dir"
Python: What's the Difference Between Pythonbrew and Virtualenv