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']
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']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With