- 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
How will you travel from child to parent with xpath in Selenium with python?
We can identify a parent from its children in DOM with the help of xpath. There are situations where we have dynamic attributes for the parent node in html but the child nodes have unique static attributes for identification.
This can be achieved with the help of relative xpath along with the parent xpath axe. Method.
Syntax
driver. find_element_by_xpath("//input[@id='job']/parent::div")
Example
Code Implementation for child to parent traversal.
from selenium import webdriver #browser exposes an executable file #Through Selenium test we will invoke the executable file which will then #invoke #actual browser driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") # to maximize the browser window driver.maximize_window() #get method to launch the URL driver.get("https://www.tutorialspoint.com/tutor_connect/index.php") #to refresh the browser driver.refresh() # identifying the edit box with the help of xpath parent atb=driver.find_element_by_xpath ("//input[@id='txtSearchText']/parent::div") #prints the class attribute value in console val = atb.get_attribute("class") print(val) driver.close()
- Related Articles
- How to use text() in xpath in Selenium with python?
- How will you handle alerts in Selenium with python?
- What is xpath in Selenium with python?
- How to use regular expressions in xpath in Selenium with python?
- How to identify nth element using xpath in Selenium with python?
- How to handle child windows in Selenium with python?
- What are the differences between xpath and css in Selenium with python?
- Style every element that is the child of its parent, counting from the last child with CSS
- How to remove all child nodes from a parent in jQuery?
- How to select specified node within Xpath node sets by index with Selenium?
- How to remove all child nodes from a parent using jQuery?
- How can you help a child with dyslexia?
- How to fetch values from a webelement in Selenium with python?
- How to select element using XPATH syntax on Selenium for Python?
- How to work with cookies in Selenium with python?

Advertisements