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

Selenium (python) : finding an element relative to a previously-found one

I’m web-scrapping a page with the selenium module in python. I’ve looked for a certain element, and found it, like this:

drv = webdriver.Firefox(options=opt)
drv.get(url)
x = drv.find_element('xpath', './/div[@class="col-lg-2 col-md-3 col-xs-6 guest-item "]')

Now I’d like to do a search for another element, using an XPath expression, but only inside the sub-tree that is below the x element, not across the entire document.

How can I specify that the XPath search should start from the x element and not from the document’s root ?

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 simply call the find_element method from the element x. For instance:

x.find_element('xpath', './/h2')
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