【问题标题】:How do I locate text using xpath?如何使用 xpath 定位文本?
【发布时间】:2022-09-24 09:09:01
【问题描述】:

我被要求改进我的问题。我是自闭症;所以我不能说谎。我确实研究了互联网,实际上不记得我在互联网上的位置,如下所示;但在这个问题上,我意识到我可以梳理一下我的互联网历史;所以在发布问题之前会更好地列出我访问的页面。

我在评论中发布了一个问题,然后立即看到回答者解决了这个问题;所以我认为他们没有看到我的评论并删除了它。后来我看到回答者实际上已经回复了我的评论;所以我会练习不删除任何东西。

我也将继续为该网站做出贡献。我现在正忙于我的项目。

下面的原始问题

我正在尝试单击 IMDB 网站上我的帐户中的“包容性”按钮,如下图所示。

我尝试了各种 selenium xpath 值的组合,并搜索了各种链接以尝试实现这一点,包括 this。我当前的 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 可以正常工作,因为我想说明 button3 所需的实际 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

我之前问过类似的question;但这与节目分级有关,该节目的 HTML 设置完全不同

    标签: python selenium xpath webdriver webdriverwait


    【解决方案1】:

    您的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')]
    

    【讨论】:

    • 20 表示 20 秒。我相信这已经足够了。我也更新了强制属性,现在试试。
    猜你喜欢
    • 2021-04-07
    • 2017-01-07
    • 1970-01-01
    • 2013-10-14
    • 2021-05-18
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    相关资源
    最近更新 更多