首页 > 解决方案 > 无法使用硒单击 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。我还尝试了隐式和显式等待,以给网页加载一些时间,但无济于事。
谁能告诉我这可能是什么原因?

标签: pythonselenium-chromedriver

解决方案


推荐阅读