Select Parent Element of Known Element in Selenium



We can select the parent element of a known element with Selenium webdriver. First of all we have to identify the known element with the help of any of the locators like id, classname and so on. Then we have to identify its parent element with findElement(By.xpath()) method.

We can identify the parent element from its child by localizing it with the child and then passing ( ./..) as a parameter to the findElement(By.xpath()). Let us identify the parent element with tagname ul from the child element with the tagname li in below html code −

Also we can identify the parent element of a known element with the help of Javascript Executor. We have to pass the Javascript command return arguments[0].parentNode and the webelement as parameters to the method executeScript. The parentNode command in Javascript is used to point to the parent of an element.

Example

Code Implementation with xpath

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class ParentElement{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      driver.get("https://www.tutorialspoint.com/about/about_careers.htm");
      // identify element
      WebElement c=driver.findElement(By.xpath("//li[@class='heading']"));
      //identify parent element with ./.. expression in xpath
      WebElement p = c.findElement(By.xpath("./.."));
      //getTagName to get tagname of parent
      System.out.println("Parent tagname is: " + p.getTagName());
      driver.close();
   }
}

Code Implementation with Javascript Executor.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
public class ParentElementJS{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      driver.get("https://www.tutorialspoint.com/about/about_careers.htm");
      // identify element
      WebElement c=driver.findElement(By.xpath("//li[@class='heading']"));
      //identify parent element parentNode expression in Javascript command
      WebElement p = (WebElement) ((JavascriptExecutor) driver).executeScript( "return arguments[0].parentNode;", c);
      //getTagName to get tagname of parent
      System.out.println("Parent tagname is: " + p.getTagName());
      driver.close();
   }
}

Output

Updated on: 2020-10-26T06:22:49+05:30

14K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements