python - 无法使用硒单击 IMDB 的“加载更多”按钮
问题描述
我正在尝试使用 selenium 从 IMDB 网站上抓取用户评论。为了加载更多评论,我实例化了一个单击事件,该事件单击评论页面上的“加载更多”按钮。问题是按钮在某些电影的评论页面上被点击,但在其他电影的评论页面上不起作用。
举个例子,下面给出的代码在“钢铁侠”用户评论网页上单击按钮时有效,但在“世界末日”网页上,它给出了错误:NoSuchElementException:消息:没有这样的元素:无法定位元素:{"method":"css selector","selector":".ipl-load-more__button"}
我觉得这有点奇怪,因为 web 元素是相同的,所以如果它适用于一个页面,它应该适用于所有页面。但是,我无法解决此问题。
这是实例化单击事件的代码:
def extend_page(wd): # wd is the webdriver
count = 20
while(count>0):
button = wd.find_element_by_class_name('ipl-load-more__button')
button.click()
wd.execute_script('window.scrollTo(0,document.body.scrollHeight);')
time.sleep(3)
new_height = wd.execute_script('return document.body.scrollHeight')
prev_height = new_height
count-=1
page = wd.page_source
return page
我尝试了不同的方法来实例化单击事件,例如 find_element_by_name 和 find_element_by_xpath。我还尝试了隐式和显式等待,以给网页加载一些时间,但无济于事。
谁能告诉我这可能是什么原因?
解决方案
推荐阅读
- javascript - JS中使用数组拉取和填充简单数据,更高效或“正确”
- javascript - 在 React 中将状态数组从一个组件传递到另一个组件
- python - 用于 QLineEdit 的 PyQt5 QCompleter 崩溃,没有异常可见
- wso2 - 在 WSO2 身份服务器中启用 totp 身份验证的“记住我”不起作用
- php - XPath 不检索某些内容
- javascript - 重新加载 Angular 子组件将用户带到 localhost:4200
- excel - Excel Sumproduct 公式分段工作,但不能一起工作
- java - maven-junit 类测试的 IDEA 社区版本限制
- sql - 在雪花程序中转义特殊字符
- python - 使用 numpy 创建具有任意形状的单位矩阵