Back

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

driver.find_element_previous?

eg:

<tr>

  <td> 

     <select>

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

        <option value=1, > </option>

        <option value=2,> </option>

     </select>

   </td>

   <td> 'abcd'

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

    <td>

<tr>

I've tried like below, but fail:

input_el=driver.find_element_by_name('A')

td_p_input=find_element_by_xpath('ancestor::input')

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')]"));

button.Click();

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.

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94k users

Browse Categories

...