Select parent element of known element in Selenium.

SeleniumAutomation TestingTesting Tools

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

raja
Published on 26-Oct-2020 06:22:28
Advertisements