How to find parent elements by python webdriver?

PythonSeleniumXpathSelenium WebdriverParent Child

Python Problem Overview


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 input element and then, finally, get the option selected?

Python Solutions


Solution 1 - Python

You can find a parent element by using .. xpath:

input_el = driver.find_element_by_name('A')
td_p_input = input_el.find_element_by_xpath('..')

What about making a separate xpath for getting selected option, like this:

selected_option = driver.find_element_by_xpath('//option[@selected="selected"]')

Solution 2 - Python

From your example, I figure you only want the selected option within a table-row if and only if this row also has an input element with the name "A", no matter where in the html-tree this element resides below the row-element.

You can achieve this via the xpath ancestor-axis.

For the sake of better readability I will show how to do this step by step (but you can actually put everything in only one xpath-expression):

# first find your "A" named element
namedInput = driver.find_element_by_name("A");
		
# from there find all ancestors (parents, grandparents,...) that are a table row 'tr'
rowElement = namedInput.find_element_by_xpath(".//ancestor::tr");
		
# from there find the first "selected" tagged option
selectedOption = rowElement.find_element_by_xpath(".//option[@selected='selected']");

Solution 3 - Python

One of the possible ways to navigate to element under same hierarchy is to use /../ in xpath as shown below:

current_element = driver.find_element_by_xpath('//android.view.ViewGroup/android.widget.RelativeLayout/android.widget.TextView[@text="Current element text"]/../android.widget.TextView[@text="Next element text"]')

Here it will:

  1. Firstly navigate to android.widget.TextView[@text = "Current element text"]
  2. Then it will go back to parent element i.e android.widget.RelativeLayout and select the next android.widget.TextView[@text="Next element text"] under the same hierarchy.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStellaView Question on Stackoverflow
Solution 1 - PythonalecxeView Answer on Stackoverflow
Solution 2 - PythondrkthngView Answer on Stackoverflow
Solution 3 - PythonParth NaikView Answer on Stackoverflow