Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xpath preceding-sibling not working

Tags:

xpath

I am trying to click on the first button which deletes the item which is the second button.

<tr>
  <td>
    <div class="btn-group">
       <button class="btn btn btn-danger" name="delete" type="button">
    </div>
  </td>
  <td>
     <span class="class"></span>
  </td>
  <td>
     <button class="btn" name="item" type="button">
  </td>
</tr>

XPath

//button[contains(.,'${ITEM}')]/preceding-sibling::button[@name='delete']
like image 480
jquerynoob Avatar asked Oct 24 '25 15:10

jquerynoob


1 Answers

I'm going to assume you're just not showing us the button text in your example HTML, since neither of your buttons seems to have any content.

preceding-sibling would not work here, since the two buttons are not siblings. However preceding::button should work in this case. Note the [1] at the end which is needed in order to select the closest match:

//button[contains(.,'${ITEM}')]/preceding::button[@name='delete'][1]

The following should also work, and is in my opinion a bit cleaner:

//tr[.//button[contains(., '${ITEM}')]]//button[@name  ='delete']
like image 179
JLRishe Avatar answered Oct 28 '25 02:10

JLRishe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!