Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find parent elements by python webdriver?

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?

like image 857
Stella Avatar asked Aug 06 '13 12:08

Stella


People also ask

How do you find parent element in Selenium?

We can select the parent element of a known element with Selenium webdriver. First of all we have to identify the known element with the help of any of the locators like id, classname and so on. Then we have to identify its parent element with findElement(By. xpath()) method.

Where is parent child xpath in Selenium?

XPath(Current node): //input[@id = 'text']. We will then find the XPath of the parent element node of the current node using parent syntax, as shown below screenshot. XPath of the Parent node: //input[@id = 'text']//parent::span. It will then select the parent node of the input tag having Id = 'text'.

How do I find my ancestors in Selenium?

We can use the 'ancestor' function to fetch a web element that is an ancestor to the current web element or in other words, if we know the parent element then we can locate a web element that can use the ancestor attribute of the XPath. Following is the syntax to use 'ancestor' function within the XPath.

What is parent and child in xpath?

We can identify a parent from its children in DOM with the help of xpath. There are situations where we have dynamic attributes for the parent node in html but the child nodes have unique static attributes for identification. This can be achieved with the help of relative xpath along with the parent xpath axe.


2 Answers

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"]') 
like image 162
alecxe Avatar answered Oct 10 '22 08:10

alecxe


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']"); 
like image 25
drkthng Avatar answered Oct 10 '22 10:10

drkthng