- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
How to find parent elements by python webdriver?
We can find parent elements with Selenium webdriver. First of all we need to identify the child element with help of any of the locators like id, class, name,xpath or css. Then we have to identify the parent element with the find_element_by_xpath() method.
We can identify the parent from the child, by localizing it with the child and then passing (..) as a parameter to the find_element_by_xpath().
Syntax−
child.find_element_by_xpath("..")
Let us identify class attribute of parent ul from the child element li in below html code−
The child element with class heading should be able to get the parent element having toc chapters class attribute.
Example
from selenium import webdriver driver = webdriver.Chrome(executable_path=" C:\chromedriver.exe") driver.implicitly_wait(0.5) driver.get("https://www.tutorialspoint.com/about/about_careers.htm") #identify child element l= driver.find_element_by_xpath("//li[@class='heading']") #identify parent from child element with (..) in xpath t= l.find_element_by_xpath("..") # get_attribute() method to obtain class of parent print("Parent class attribute: " + t.get_attribute("class")) driver.close()
Output
Advertisements