
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Travel from Child to Parent with XPath in Selenium using 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()
Advertisements