簡體   English   中英

selenium xpath 無法在 web 頁面上找到文本

[英]selenium xpath unable to locate text on web page

我正在嘗試單擊 IMDB 網站上我的帳戶中的“包容性”按鈕,如下圖所示。

我嘗試了 selenium xpath 值的各種組合,並搜索了各種鏈接以嘗試實現這一點,包括這個 我當前的 python 低於登錄 IMDB 網站以實現此更改。

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

# click on arrow button in bottom right hand corner of image further below
#  to call up pop-up window wth lists like 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

按鈕 1 和 2 工作,因為我想說明我需要的按鈕 3 的實際 xpath 也應該工作。 最終代碼中不需要按鈕 1 和 2,它們只是為了說明它們至少是分開工作的。 我試圖抓取的 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>

python 當前不產生錯誤消息,但從 button3 = driver.find_element(By.XPATH, xpath) 跳轉到 except 子句

我正在使用 PyCharm 2022.2.2 Build #PC-222.4167.33 和 Python 3.10.7 和 chromedriver win32 105.0.5195.52

我之前問過類似的問題 但這與節目分級有關,該節目具有完全不同的 HTML 設置

在此處輸入圖像描述

您的xpath似乎是錯誤的。 使用下面的xpath點擊Inclusive按鈕

//div[@role='button' and @data-titleinlist='false'][contains(., 'Inclusive')]

所以你的代碼會像

xpath = "//div[@role='button' and @data-titleinlist='false'][contains(., 'Inclusive')]"
button3 = driver.find_element(By.XPATH, xpath)
driver.execute_script("arguments[0].click();", button3)

請提供足夠的等待以使元素可見或可點擊。

您也可以使用xpath以下。

//div[@class='sc-1aecbe70-0 dpbfLr']//div[@role='button'][contains(., 'Inclusive')]

你也可以使用它。

//div[@class='sc-1aecbe70-0 dpbfLr']//div[@role='button' and @data-titleinlist='false'][contains(., 'Inclusive')]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM