How to use XPath preceding-sibling correctly

jquerynoob picture jquerynoob · May 8, 2014 · Viewed 185.8k times · Source

I am writing tests for my site using Selenium IDE and I am having trouble with having selenium click on a button using preceding-sibling

<td>
<div class="btn-group">
<button class="btn btn btn-danger block" title="Warning, Delete" name="delete" type="button">
<button class="btn btn btn-default block" title="View History" name="history" type="button">
<button class="btn btn btn-default block" title="View Settings" name="settings" type="button">
<button class="btn btn btn-default block" name="device" type="button">
<span class="glyphicon glyphicon-pencil"/>
 Arcade Reader
</button>
</div>
</td>

My path

xpath=//button[contains(.,'Arcade Reader')]/../preceding-sibling::button[@name='settings']

Answer

alecxe picture alecxe · May 8, 2014

You don't need to go level up and use .. since all buttons are on the same level:

//button[contains(.,'Arcade Reader')]/preceding-sibling::button[@name='settings']