使用子元素的文本获取父元素的 XPath

Getting XPath of parent element using using text of child element

给定一个像这样的 XML 结构:

<ul>
    <li role="treeitem" aria-level="1" aria-expanded="true">
        <span role="presentation"></span>
        <span>
            <span>Seasons</span>
        </span>
    </li>
    <ul>
        <li role="treeitem" aria-level="2" aria-expanded="false">
            <span role="presentation"></span>
            <span>
                <span>Winter months</span>
            </span>
        </li>
        <ul></ul>
        <li role="treeitem" aria-level="2" aria-expanded="true">
            <span role="presentation"></span>
            <span>
                <span>Spring months</span>
            </span>
        </li>
        <ul >
            <li role="treeitem" aria-level="3" >
                <span>
                    <span>March</span>
                </span>
            </li>
            <ul></ul>
            <li role="treeitem" aria-level="3">
                <span>
                    <span>April</span>
                </span>
            </li>
            <ul></ul>
            <li role="treeitem" aria-level="3">
                <span>
                    <span>May</span>
                </span>
            </li>
            <ul></ul>
        </ul>
        <li role="treeitem" aria-level="2" aria-expanded="false">
            <span role="presentation"></span>
            <span>
                <span>Summer months</span>
            </span>
        </li>
        <ul></ul>
        <li role="treeitem" aria-level="2" aria-expanded="false">
            <span role="presentation"></span>
            <span>
                <span>Autumn months</span>
            </span>
        </li>
        <ul></ul>
    </ul>
</ul>

其中<span role="presentation"></span>是按钮,点击后会打开一个树枝。 aria-expanded = 'true'表示已开通分行。

  1. 我如何获取包含子标签内文本 'Winter months'li 元素的路径(保存在 String xpathToLi 中)?
  2. 如何从找到的路径 xpathToLi 中获取 the aria-expanded 属性的值?
  3. 如果 aria-expanded = 'false'xpathToLi 路径内,在 xpathToLi 中找到带有 role = 'presentation' 的元素并单击它。

假设您正在使用 java。

  1. 如何获取子标签内包含文本 'Winter months' 的 li 元素的路径(将其保存在 String xpathToLi 中)?

WebElement xpathToLi = driver.findElement(By.xpath("//span[text()='Winter months']/ancestor::li"));

  1. 如何从找到的路径 xpathToLi 中获取 aria-expanded 属性的值?

String ariaExpanded = xpathToLi.getAttribute("aria-expanded");

  1. 如果 aria-expanded = 'false' 在 xpathToLi 路径中,在 xpathToLi 中找到角色 = 'presentation' 的元素并单击它。

if(ariaExpanded.equalsIgnoreCase("false")) { xpathToLi.findElement(By.xpath("./span[@role='presentation']")).click(); }

要提取 aria-expanded 季节的属性值,即 冬季月份 ,您可以使用以下解决方案:

  • Java 基于xpath 解决方案:

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//ul//span[text()='Seasons']//following::ul[1]//span[text()='Winter months']//preceding::li[1]"))).getAttribute("aria-expanded"));
    
  • Python 基于xpath 解决方案:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//ul//span[text()='Seasons']//following::ul[1]//span[text()='Winter months']//preceding::li[1]"))).get_attribute("aria-expanded"))