简体   繁体   中英

find out all child elements xpath from parent xpath using selenium webdriver in python

I can find the element by xpath of driver.find_element_by_xpath('//*[@id="app"]/table/tbody/tr[1]/td[1]') . but any way i can return all children elements like tag and tag xpath?

<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>

The key is to use the xpath .//* to get all the children for current node. The . picks the current element & //* selects all the elements, which makes the whole xpath to select all child elements of 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('.//*')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM