Issue
I tried to scroll down to the element of the page by using following code:
# coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Firefox()
url = "https://selenium-python.readthedocs.io/navigating.html"
driver.get(url)
web_element = driver.find_element(By.ID, "filling-in-forms")
actions = ActionChains(driver)
actions.scroll_to_element(web_element)
actions.perform()
But this code yields an error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python310\lib\site-packages\selenium\webdriver\common\action_chains.py", line 78, in perform
self.w3c_actions.perform()
File "C:\Python310\lib\site-packages\selenium\webdriver\common\actions\action_builder.py", line 88, in perform
self.driver.execute(Command.W3C_ACTIONS, enc)
File "C:\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 435, in execute
self.error_handler.check_response(response)
File "C:\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: unknown variant `wheel`, expected one of `none`, `key`, `pointer` at line 1 column 226
How can I fix the problem?
Solution
scroll_to_element()
scroll_to_element(element: selenium.webdriver.remote.webelement.WebElement)
: If the element is outside the viewport, scrolls the bottom of the element to the bottom of the viewport.
It is defined as:
def scroll_to_element(self, element: WebElement):
"""
If the element is outside the viewport, scrolls the bottom of the element to the bottom of the viewport.
:Args:
- element: Which element to scroll into the viewport.
"""
self.w3c_actions.wheel_action.scroll(origin=element)
return self
As per the API docs I don't see any issue/error in your code block. However when I execute similar code block using Selenium 4.1.0 using ChromeDriver and Chrome:
web_element = driver.find_element(By.ID, "filling-in-forms")
ActionChains(driver).scroll_to_element(web_element).perform()
I face a different error as follows:
4.1.0
Traceback (most recent call last):
File "C:\Users\debanjan.bhattacharj\Desktop\Python Programs\Selenium_Chrome_Service.py", line 30, in <module>
ActionChains(driver).scroll_to_element(web_element).perform()
AttributeError: 'ActionChains' object has no attribute 'scroll_to_element'
Seems there is a bug in the method defination/implementation.
Solution
If your use case is to scroll to an element as an alternative you can use scrollIntoView()
inducing WebDriverWait for the for the visibility_of_element_located()
as follows:
driver.get("https://selenium-python.readthedocs.io/navigating.html")
web_element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.ID, "filling-in-forms")))
driver.execute_script("return arguments[0].scrollIntoView(true);", web_element)
Answered By - undetected Selenium
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.