- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
- Related Articles
- How to obtain the tagname of the parent element in Selenium webdriver?
- Select every element that is the second element of its parent, counting from the last child
- Place an element in the middle of the parent element in Bootstrap 4
- How to select element using XPATH syntax on Selenium for Python?
- Accessing a parent Element using JavaScript
- Style every element that is the first element of its parent with CSS
- Style every element that is the last element of its parent with CSS
- Style every element that is the nth element of its parent with CSS
- Style every element that is the only element of its parent with CSS
- Finding an element in a sub-element in Selenium Webdriver.
- Align an element with the bottom of the parent element's font in Bootstrap 4
- Align an element with the top of the parent element's font in Bootstrap 4
- How to return true if the parent element contains the child element in JavaScript?
- How to inherit CSS properties of parent Element using JavaScript?
- Canva – How to select an element behind another element
