简体   繁体   中英

Unable to find element by XPath if I use following-sibling::

I am using the following XPath to locate my element:

//tbody/*[contains(.,'abcabc')]/following-sibling::div[1]/a[@class="editbtn"]

//tbody/*[contains(.,'abcabc')] fetches me the element so far.

However, when I use /following-sibling::div[1]/a[@class="editbtn"] after that, or even /following-sibling::div[1] , I get:

Unable to locate element:

My HTML looks like:

<tbody>
    <tr>
        <td>
            <div>
                 <a href="#!/service/1/policies" data-id="1">abcabc</a>
                 <div class="pull-right">
                     <a class="editbtn" title="Edit" href="href1" data-id="1">
                         <i class="icon-edit"/>
                     </a>
                     <a class="deletebtn" title="Delete" href="javascript:void(0);" data-id="1">
                         <i class="icon-trash"/>
                     </a>
                 </div>
             </div>
         </td>
     </tr>
</tbody>

When you look for //tbody/*[contains(.,'abcabc')] , you're not selecting what you think you're selecting.

You think you're selecting the a element that contains 'abcabc', but you're actually selecting the tbody itself because its string value also contains 'abcabc'. The tbody element has no following siblings, so you get nothing.

To select the div following the a in your example, specify the //a rather than using * to match any element:

//tbody//a[contains(.,'abcabc')]/following-sibling::div[1]

That will resolve your following-sibling issue and select the class="pull-right" div that follows the abcabc a element.

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