Select the parent of a known item in Selenium

I have a specific item that I can select with Selenium 1.

Unfortunately, I need to click on the parent element to get the desired behavior. An element that I can easily find has an unselectable attribute, making it dead for a click. How to go up using XPath ?

+80
select xpath selenium parent
Dec 20 '11 at 3:18 a.m.
source share
7 answers

There are a couple of options. The sample code is in Java, but the port to other languages ​​should be simple.

JavaScript:

WebElement myElement = driver.findElement(By.id("myDiv")); WebElement parent = (WebElement) ((JavascriptExecutor) driver).executeScript( "return arguments[0].parentNode;", myElement); 

XPath:

 WebElement myElement = driver.findElement(By.id("myDiv")); WebElement parent = myElement.findElement(By.xpath("..")); 

Getting driver from WebElement

Note. . As you can see, for the JavaScript version you will need a driver . If you do not have direct access to it, you can get it from WebElement using:

 WebDriver driver = ((WrapsDriver) myElement).getWrappedDriver(); 
+130
Aug 01 '13 at 18:21
source share

A bit more about XPath axes

Let's say we have the following HTML structure:

 <div class="third_level_ancestor"> <nav class="second_level_ancestor"> <div class="parent"> <span>Child</span> </div> </nav> </div> 
  1. //span/parent::* - returns any element that is a direct parent.

In this case, the output will be <div class="parent">

  1. //span/parent::div[@class="parent"] - returns the parent element of only the exact type of the node and only if the specified predicate is True.

Output: <div class="parent">

  1. //span/ancestor::* - returns all the ancestors (including the parent).

Output: <div class="parent"> , <nav class="second_level_ancestor"> , <div class="third_level_ancestor"> ...

  1. //span/ancestor-or-self::* - returns all the ancestors and the current element itself.

Output: <span>Child</span> , <div class="parent"> , <nav class="second_level_ancestor"> , <div class="third_level_ancestor"> ...

  1. //span/ancestor::div[2] - returns the second ancestor (starting from the parent) of the div type.

Output: <div class="third_level_ancestor">

+20
Aug 19 '17 at 9:16 on
source share

Take a look at the possible XPath axes , you are probably looking for parent . Depending on how you find the first element, you can simply configure xpath for this.

Alternatively, you can try the two-point syntax , .. , which selects the parent of the current node.

+13
Dec 20 '11 at 16:01
source share

Let's look at your DOM as

 <a> <!-- some other icons and texts --> <span>Close</span> </a> 

Now that you need to select the parent tag 'a' based on the text <span> , use

 driver.findElement(By.xpath("//a[.//span[text()='Close']]")); 

Explanation: Select a node based on the value of its child node.

+12
Mar 23 '17 at 17:04 on
source share

This might be useful for someone else: Using this HTML example

 <div class="ParentDiv"> <label for="label">labelName</label> <input type="button" value="elementToSelect"> </div> <div class="DontSelect"> <label for="animal">pig</label> <input type="button" value="elementToSelect"> </div> 

If, for example, I want to select an element in the same section (e.g. div) as a label, you can use this

 //label[contains(., 'labelName')]/parent::*//input[@value='elementToSelect'] 

It just means looking for a label (it could be something like a , h2 ) named labelName . Go to the parent of this label (that is, div class="ParentDiv" ). elementToSelect search among the descendants of this parent to find any child element with the value elementToSelect . However, he will not select the second elementToSelect with the DontSelect div as the parent.

The trick is that you can reduce the search area for an element by going to the parent first and then searching for the desired element in the descendant of that parent. Other syntax such as following-sibling::h2 may also be used in some cases. This means that brother and sister follow element h2 . This will work for items of the same level having the same parent.

+4
Jan 25 '17 at 14:16
source share

You can do this using / parent :: node () in xpath. Just add / parent :: node () to the xpath children.

For example: Let xpath of the child element be the path of childElementX.

Then the xpath of its immediate ancestor will be childElementXpath / parent :: node ().

Xpath of its next ancestor will be childElementXpath / parent :: node () / parent :: node ()

etc.

Alternatively, you can go to the ancestor of the element using 'childElementXpath/ancestor::*[@attr="attr_value"]' . This would be useful if you have a child that is unique but has a parent that cannot be uniquely identified.

0
Dec 08 '16 at 4:38
source share

We can select the parent tag using Selenium as follows:

 driver.findElement(By.xpath("//table[@id='abc']//div/nobr[.='abc']/../..")); 

this will help you find the grandfather and grandmother of the famous Element. Just delete one (/ ..) to find the closest parent.

how

 driver.findElement(By.xpath("//table[@id='abc']//div/nobr[.='abc']/..)); 

There are other ways to implement this, but it worked fine for me.

0
Sep 11 '17 at 9:29 on
source share



All Articles