Find out all xpath children from parent xpath using selenium webdriver in python
I can find an element by xpath driver.find_element_by_xpath('//*[@id="app"]/table/tbody/tr[1]/td[1]')
. but is there any way I can get all child elements like tag and xpath tag back?
<tr>
<td class="">
<div>
<a href="/user/1">
<!-- react-text: 6011 -->user first name |
<!-- /react-text -->
<!-- react-text: 6012 -->
<!-- /react-text -->
<!-- react-text: 6013 -->user last name
<!-- /react-text -->
</a>
<div>
<span>
<!-- react-text: 6014 -->town<!-- /react-text -->
<!-- react-text: 6015 --> | <!-- /react-text -->
<!-- react-text: 6015 -->month<!-- /react-text -->
<!-- react-text: 6081 --> | date<!-- /react-text -->
<!-- react-text: 6082 -->year<!-- /react-text -->
</span>
</div>
</div>
</td>
<td class=""><a href="/address/1">1</a>
<div class="">city</div>
</td>
</tr>
+3
source to share
1 answer
The key is to use xpath .//*
to get all the children for the current node. .
selects the current element and //*
selects all elements, which makes the whole xpath to select all children of the current element. Your code will look like this:
parent_elem = driver.find_element_by_xpath('//*[@id="app"]/table/tbody/tr[1]/td[1]')
child_elements = parent_elem.find_elements_by_xpath('.//*')
+3
source to share