How to use XPath preceding-sibling correctly

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> <button title="Warning, Delete" name="delete" type="button"> <button title="View History" name="history" type="button"> <button title="View Settings" name="settings" type="button"> <button name="device" type="button"> <span/> Arcade Reader </button> </div> </td> 

My path

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

2 Answers

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'] 
0

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(); 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like