【问题标题】:selenium.common.exceptions.JavascriptException: Message: TypeError: document.getElementsByClassName(...)[0] is undefinedselenium.common.exceptions.JavascriptException:消息:TypeError:document.getElementsByClassName(...)[0] 未定义
【发布时间】:2020-09-11 13:43:49
【问题描述】:

我试图让程序滚动到特定的div 以加载其中包含的所有需要​​的元素。据我所知,JavaScript 代码是正确的,但它一直给我这个错误:

selenium.common.exceptions.JavascriptException: Message: TypeError: document.getElementsByClassName(...)[0] is undefined

这是错误所指的行:

bot.execute_script("document.getElementsByClassName('uiScrollableAreaBody')[0].scrollTo(0,1000)")

【问题讨论】:

    标签: javascript python selenium selenium-webdriver execute-script


    【解决方案1】:

    似乎是脚本试图在页面加载之前获取元素。尝试使用显式等待或time.sleep(x)(我个人更喜欢第一种方法,这样您的脚本就不会停止 x 秒数。

    这是伪代码。

    element = WebDriverWait(driver,30).until(lambda x: x.execute_script("return document.getElementsByClassName('uiScrollableAreaBody')[0]"))
    # now you can perform operation on the element
    

    【讨论】:

      猜你喜欢
      • 2018-11-24
      • 1970-01-01
      • 2021-04-21
      • 1970-01-01
      • 2018-11-18
      • 2020-06-27
      • 2021-06-20
      • 2020-02-22
      相关资源
      最近更新 更多