繁体   English   中英

javascript 错误:arguments[0].scrollTo 不是 function 在 python 上使用 Selenium

[英]javascript error: arguments[0].scrollTo is not a function using Selenium on python

使用 selenium 启动我的 python 脚本时,我收到以下错误。

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

我不确定这里到底发生了什么 - 我会提供更多细节,但这是我正在运行的程序,这个错误来自哪里。

  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))

如果您发现任何不合适的地方,请提供帮助。

我试过将这部分注释掉 - 但它弄乱了整个脚本。

Javascript 不能在任何 WebElement 上调用scrollTo() ,只能在Window object 上调用。


解决方案

可以有两种方法:

  • 要么你scrollIntoView()元素:

     self.browser.execute_script("return arguments[0].scrollIntoView(true);", scrollable_element)
  • 或者您在Window object 上调用scrollTo()

     self.browser.execute_script("window.scrollTo(0, {});".format(i))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM