Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in DevOps and Agile by (19.7k points)

Is there any methods for python+selenium to find parent elements, brother elements, or child elements just like

driver.find_element_parent? or

driver.find_element_next? or






        <option value=0, selected='selected'> </option> 

        <option value=1, > </option>

        <option value=2,> </option>



   <td> 'abcd'

     <input name='A'> </input>



I've tried like below, but fail:



How can I get the parent of the input element and then, finally, get the option selected?

1 Answer

0 votes
by (62.9k points)

Use the following code:

IWebElement button = CurrentBrowser.FindElement(By.XPath("//span[@class='originalProductPrice']/ancestor::li//a[contains(@class,'btn-primary')]"));


The portion of note in this XPath is /ancestor::li. The XPath works by first finding the <span> element with class equal to "originalProductPrice", then searching for your button from the nearest <li> element that is a parent of that span.

Browse Categories