0

I'm receiving the following error when launching my python script with selenium.

File "/usr/local/lib/python3.10/dist-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.JavascriptException: Message: javascript error: arguments[0].scrollTo is not a function

I'm not sure what exactly is going on here - I would give more detail, but here is the program I'm running where this error is coming from.

  def scroll_slow(self, scrollable_element, start=0, end=3600, step=100, reverse=False):
        if reverse:
            start, end = end, start
            step = -step

        for i in range(start, end, step):
            self.browser.execute_script("arguments[0].scrollTo(0, {})".format(i), scrollable_element)
            time.sleep(random.uniform(1.0, 2.6))

If you see anything out of place, please help.

I've tried commenting this section out - but it messes up the whole script.

Shawn
  • 4,064
  • 2
  • 11
  • 23

1 Answers1

0

Javascript can't invoke scrollTo() on any WebElement but on the Window object.


Solution

There can be two approaches:

  • Either you scrollIntoView() the element:

    self.browser.execute_script("return arguments[0].scrollIntoView(true);", scrollable_element)
    
  • Or you invoke scrollTo() on the Window object:

    self.browser.execute_script("window.scrollTo(0, {});".format(i))
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352