繁体   English   中英

如何获取祖先<a>标签相对于已故孩子</a>的 href 属性<h3>标签使用 Selenium 和 Python</h3>

[英]How to get the href attribute of the ancestor <a> tag with respect to the decedent child <h3> tag using Selenium and Python

我正在尝试获取一个<a>元素,以防其嵌套(内部) <h3>标记包含特定文本。 我该怎么做? 假设结构如下:

<a href="https://example.com">
    <section class="section">
        <div class="section-content">
            <div class="section-inner">
                <h3>
                    Are you searching for specific keyword here?
                </h3>
            </div>
        </div>
    </section>
</a>

此 HTML 是较长的 HTML 文本的一部分,该文本都位于<body>标记内。 这意味着源 HTML 具有该结构的倍数,因此可以迭代这些结构。

搜索字符串是“你正在搜索”,所以应该匹配元素。 匹配元素后,我想获得一个标签的href值,即: https://example.com

目前,在玩过这个之后,我知道如何通过调整其中的文本来匹配h3标签,但不知道在匹配完成后如何获取父<a>标签的href

elem = driver.find_elements_by_xpath("//h3[contains(text(), 'you searching')]")
# elem is h3 tag...

检索<a>标记相对于<h3>标记文本的href属性值您是否在此处搜索特定关键字? 您可以使用以下任一定位器策略

  • 使用xpathnormalize-space()

     print(driver.find_element_by_xpath("//h3[normalize-space()='Are you searching for specific keyword here?']//ancestor::a[1]").get_attribute("href"))
  • 使用xpathcontains()

     print(driver.find_element_by_xpath("//h3[contains(., 'Are you searching for specific keyword here?')]//ancestor::a[1]").get_attribute("href"))

理想情况下,您需要为visibility_of_element()引入WebDriverWait ,并且可以使用以下任一Locator Strategies

  • 使用xpathnormalize-space()

     print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h3[normalize-space()='Are you searching for specific keyword here?']//ancestor::a[1]"))).get_attribute("href"))
  • 使用xpathcontains()

     print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h3[contains(., 'Are you searching for specific keyword here?')]//ancestor::a[1]"))).get_attribute("href"))
  • 注意:您必须添加以下导入:

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM