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 the 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?
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:
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:
I face a different error as follows:
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 thevisibility_of_element_located()as follows: