How to use XPath preceding-sibling correctly
234,836
Solution 1
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']
Solution 2
I also like to build locators from up to bottom like:
//div[contains(@class,'btn-group')][./button[contains(.,'Arcade Reader')]]/button[@name='settings']
It's pretty simple, as we just search btn-group
with button[contains(.,'Arcade Reader')]
and get it's button[@name='settings']
That's just another option to build xPath locators
What is the profit of searching wrapper element: you can return it by method (example in java) and just build selenium constructions like:
getGroupByName("Arcade Reader").find("button[name='settings']");
getGroupByName("Arcade Reader").find("button[name='delete']");
or even simplify more
getGroupButton("Arcade Reader", "delete").click();
Author by
jquerynoob
Updated on July 09, 2022Comments
-
jquerynoob almost 2 years
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']