NeverSleeps
NeverSleeps

Reputation: 1902

Getting XPath of parent element using using text of child element

Given an XML structure like so:

<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>

where <span role="presentation"></span> are buttons, when clicked, open a tree branch. aria-expanded = 'true' means opened branch.

  1. How could I get the path to the li element containing the text 'Winter months' inside the child tags (save this in String xpathToLi)?
  2. How could I get the value of the aria-expanded attribute from the found path xpathToLi?
  3. If aria-expanded = 'false' inside the xpathToLi path, find into xpathToLi the element with role = 'presentation' and click on it.

enter image description here

Upvotes: 0

Views: 651

Answers (2)

Sooraj
Sooraj

Reputation: 585

Assuming you are using java.

  1. How could I get the path to the li element containing the text 'Winter months' inside the child tags (save this in String xpathToLi)?

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

  1. How could I get the value of the aria-expanded attribute from the found path xpathToLi?

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

  1. If aria-expanded = 'false' inside the xpathToLi path, find into xpathToLi the element with role = 'presentation' and click on it.

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

Upvotes: 1

undetected Selenium
undetected Selenium

Reputation: 193088

To extract the value of the attribute aria-expanded for the season of i.e. Winter months you can use the following solution:

  • Java based xpath solution:

    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 based xpath solution:

    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"))
    

Upvotes: 0

Related Questions