selenium.common.exceptions.JavascriptException:Message:TypeError:document.getElementsByClassName(…)[0]未定义

2024-10-17 06:19:24 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

这是错误所指的行:

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

Tags: 代码程序divmessageisselenium错误common
1条回答
网友
1楼 · 发布于 2024-10-17 06:19:24

似乎脚本正在尝试在元素加载到页面之前获取该元素。尝试使用显式等待或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

相关问题 更多 >