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 ?
>Solution :
You can simply call the find_element method from the element x. For instance:
x.find_element('xpath', './/h2')