Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to find an element with respect to its parent element with Selenium and Python

I am trying to scrape a website, but I need to search for an element whose parent is like this:

//div[@title="parent"]

People are talking about getting an element from its child. Is there a way to reverse it and find the child from its parent?

I want the /span with @title = "child" whose parent is //div[@title = "Search results."]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can try to use

//div[parent::div[@title="parent"]]

or simply

//div[@title="parent"]/div

In Python code you can also use

parent = driver.find_element(By.XPATH, '//div[@title="parent"]')
child = parent.find_element(By.XPATH, './div')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading