You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过XPath定位IMDB页面的Inclusive按钮(Selenium求助)

问题描述

我正尝试点击IMDB影片页面中的“Inclusive”按钮(按钮位置示意图如下)。

我尝试了多种Selenium XPath组合,还查阅了XPath教程。当前Python代码如下(省略IMDB登录部分):

driver.get('https://www.imdb.com/title/tt14772170/?ref_=nv_sr_srsg_0')
wait = WebDriverWait(driver, 20)

# 点击图片右下角的箭头按钮,调出包含Inclusive等列表的弹窗
xpath = "//button[@class='ipc-split-button__iconBtn']"
wait.until(EC.element_to_be_clickable((By.XPATH, xpath))).click()

try:
    match_found = True
    xpath = "//div[@class='sc-1aecbe70-0 dpbfLr']/div[@data-titleinlist='false']"
    button1 = driver.find_elements(By.XPATH, xpath)
    xpath = "//*[contains(text(), 'Inclusive')]"
    button2 = driver.find_element(By.XPATH, xpath)
    xpath = "//div[@class='sc-1aecbe70-0 dpbfLr']/div[@data-titleinlist='false']/*[contains(text(), 'Inclusive')]"
    button3 = driver.find_element(By.XPATH, xpath)
    driver.execute_script("arguments[0].click();", button3)
except selenium.common.exceptions.NoSuchElementException:
    match_found = False

button1和button2能正常找到元素,说明button3对应的XPath理论上应该有效,但实际运行时,代码从button3 = driver.find_element(By.XPATH, xpath)直接跳转到异常分支,无报错信息。button1和button2仅用于验证,最终代码中不会保留。

以下是目标元素的HTML结构:

<div class="sc-1aecbe70-0 dpbfLr">
    <div role="button" tabindex="0" data-titleinlist="false" class="sc-1aecbe70-1 fkjUqe">
        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" class="ipc-icon ipc-icon--add sc-1aecbe70-2 csaBFR" id="iconContext-add" viewBox="0 0 24 24" fill="currentColor" role="presentation">
            <path d="M18 13h-5v5c0 .55-.45 1-1 1s-1-.45-1-1v-5H6c-.55 0-1-.45-1-1s.45-1 1-1h5V6c0-.55.45-1 1-1s1 .45 1 1v5h5c.55 0 1 .45 1 1s-.45 1-1 1z"></path>
        </svg>
    Inclusive</div>
    <a href="/list/ls560615425/?ref_=tt_ov_ls_menu_sm" aria-label="Go to list: Inclusive" class="sc-1aecbe70-4 grpYsz">
        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" class="ipc-icon ipc-icon--chevron-right" id="iconContext-chevron-right" viewBox="0 0 24 24" fill="currentColor" role="presentation">
            <path fill="none" d="M0 0h24v24H0V0z"></path><path d="M9.29 6.71a.996.996 0 0 0 0 1.41L13.17 12l-3.88 3.88a.996.996 0 1 0 1.41 1.41l4.59-4.59a.996.996 0 0 0 0-1.41L10.7 6.7c-.38-.38-1.02-.38-1.41.01z"></path>
        </svg>
    </a>
</div>

我的环境:PyCharm 2022.2.2 Build #PC-222.4167.33、Python 3.10.7、chromedriver win32 105.0.5195.52。

此前我曾提问过类似问题,但那是关于为节目评分的,HTML结构完全不同。

按钮位置示意图

解决方案

问题出在你写的XPath上://div[@class='sc-1aecbe70-0 dpbfLr']/div[@data-titleinlist='false']/*[contains(text(), 'Inclusive')] 里的*是匹配子元素,但“Inclusive”文本是直接在div[@data-titleinlist='false']这个元素内部的,不是它的子元素的文本,所以这个XPath找不到。

你可以用以下几种方式修改:

  1. 直接定位包含文本的div元素,结合父容器和属性:
xpath = "//div[@class='sc-1aecbe70-0 dpbfLr']/div[@data-titleinlist='false' and contains(text(), 'Inclusive')]"
button3 = wait.until(EC.element_to_be_clickable((By.XPATH, xpath)))
button3.click()

这里用WebDriverWait等待元素可点击,比直接find_element更可靠,避免元素加载延迟的问题。

  1. 或者通过文本直接定位,同时确保是在目标容器内:
xpath = "//div[@class='sc-1aecbe70-0 dpbfLr']//div[contains(text(), 'Inclusive') and @data-titleinlist='false']"
button3 = wait.until(EC.element_to_be_clickable((By.XPATH, xpath)))
button3.click()

另外,IMDB的动态类名(比如sc-1aecbe70-0 dpbfLr)可能会变化,建议用更稳定的定位方式,比如结合role="button"和文本:

xpath = "//div[@role='button' and contains(text(), 'Inclusive') and @data-titleinlist='false']"
button3 = wait.until(EC.element_to_be_clickable((By.XPATH, xpath)))
button3.click()

这样修改后,就能正确定位到目标按钮并点击了。


内容的提问来源于stack exchange,提问作者Cyrus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 00:15:50