Selenium scroll_to_element InvalidArgumentException

Path Int.

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?

undetected Selenium

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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

PHPUnit + Selenium `InvalidArgumentException: Element not found`

selenium.common.exceptions.InvalidArgumentException

Passing a variable as argument to selenium find_element function instead of hardcoded argument results in a InvalidArgumentException

Selenium throws an InvalidArgumentException when rotating proxy

Selenium driver.page_source InvalidArgumentException

How to scroll element with Selenium?

org.openqa.selenium.InvalidArgumentException: unknown error: unsupported protocol

org.openqa.selenium.InvalidArgumentException: Invalid capabilities using DesiredCapabilities

Selenium Chrome can't see browser logs InvalidArgumentException

How to scroll to element with Selenium WebDriver

selenium.common.exceptions.InvalidArgumentException: Message: File not found while trying to upload image by url through selenium

Selenium throws InvalidArgumentException (Preference values not all string or integer or boolean) when running with Firefox

InvalidArgumentException: invalid argument and InvalidSelectorException: invalid selector error with ChromeDriver and Chrome using Selenium with Java

selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: 'using' must be a string using waits and expected conditions

I've got InvalidArgumentException when i used switch_to.window by selenium / Python

InvalidArgumentException: invalid argument: Unsupported locator strategy: -custom error with Appium using Selenium ChromeDriver and Chrome

InvalidArgumentException: invalid argument error using Selenium and Pandas scraping urls reading from a CSV

solved: python: selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator

selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: 'using' must be a string

How to scroll UP to an element and click in selenium?

how to scroll a local div element with selenium

Scroll on a specific DIV Element in Python Selenium

Scroll to specific element Selenium WebDriver Java

How to scroll to the bottom inside of an element with selenium?

Scroll and find an element using selenium python

Get scroll height or height of an element and not body selenium

Scroll on a specific DIV Element to the bottom in Python Selenium

How to smooth scroll inside element with Selenium[Python]

Locating element in splinter/selenium that require scroll