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:
Selenium element could not be scrolled into view
Just click on the parent element (with './..') and it'll work fine:
tos_checkbox = brower.find_element_by_xpath("//input[@id='mat-checkbox-1-input']/./..")
tos_checkbox.click()
Regards !
Element(button) could not be scrolled into view
The error "selenium.common.exceptions.ElementNotInteractableException: Message: Element could not be scrolled into view"
implies that the element which your program was trying to interact with could not be scrolled into view.
To overcome that you can introduce WebDriverWait
and click or use element.location_once_scrolled_into_view
first and then click on the element.
Or you can use Javascript's executor
to click on the element.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#btnSearch"))).click()
OR
ok3=driver.find_element_by_css_selector('#btnSearch')
ok3.location_once_scrolled_into_view
ok3.click()
OR
ok3=driver.find_element_by_css_selector('#btnSearch')
driver.execute_script("arguments[0].click();",ok3)
Python 3.7 Help on Selenium-Element could not be scrolled into view
Looks like this element is located out of the initially visible screen when you open that page with Selenium.
Try scrolling it into the view and then clicking it with the following code:
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)
pager = driver.find_element_by_xpath('//li[@class="next"]')
actions.move_to_element(pager).perform()
time.sleep(0.5)
pager.click()
Python Selenium could not be scrolled into view
The error suggest that you need to scrolled to the element.You can achieve that in selenium.
Try below options.
1 Use javascript
executor to scroll.
WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,"//div[@data-field-name='incident_type_ids']"))).click()
element=WebDriverWait(driver,30).until(EC.presence_of_element_located((By.XPATH,"//ul[@class='chosen-results']//li[contains(.,'Phishing')]")))
driver.execute_script("arguments[0].scrollIntoView()", element)
element.click()
2 Use selenium property location_once_scrolled_into_view
.
WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,"//div[@data-field-name='incident_type_ids']"))).click()
element=WebDriverWait(driver,30).until(EC.presence_of_element_located((By.XPATH,"//ul[@class='chosen-results']//li[contains(.,'Phishing')]")))
element.location_once_scrolled_into_view
element.click()
If you get any error something like NOT clickable at this point then use JS to click on the element.
WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,"//div[@data-field-name='incident_type_ids']"))).click()
element=WebDriverWait(driver,30).until(EC.presence_of_element_located((By.XPATH,"//ul[@class='chosen-results']//li[contains(.,'Phishing')]")))
element.location_once_scrolled_into_view
driver.execute_script("arguments[0].click();", element)
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)
Related Topics
Why Do I Need to Deploy a "Default" App Before I Can Deploy Multiple Services in Gae
How to Enumerate a Range of Numbers Starting at 1
Import Module Works in Terminal But Not in Idle
Exponentials in Python: X**Y VS Math.Pow(X, Y)
Lambda Function Don't Closure the Parameter in Python
When Should Iteritems() Be Used Instead of Items()
Pyplot Move Alternative Y Axis to Background
What Is the Meaning of "Failed Building Wheel for X" in Pip Install
Nltk Named Entity Recognition to a Python List
Keep Same Dummy Variable in Training and Testing Data
Python - Using the Multiply Operator to Create Copies of Objects in Lists
Finding a Substring Within a List in Python
"Private" (Implementation) Class in Python
Preserving Global State in a Flask Application
Sorting a Dictionary with Lists as Values, According to an Element from the List
Python Regular Expression Pattern * Is Not Working as Expected