Totally agree with @ThomasWalpole
. Though first Xpath expression is relative, and the second one is absolute, they are both not a good case to use. Even the smallest change in page code, that may not be noticed by any other way, while working with your page under test, will (with a great probabibility) ruin both of these expressions. What kind of expression should be used, depends on what is the purpose of your testing. As I can see, you are targeting a button element. This gives me an idea, that you'll try to click this button with some kind of test automation tool, and check if the result. If this is the case, I would recommend you to implement the logic you are following when clicking a button as a human. You are examining the text/icon on the button, and maybe it's color or something. So try something like //button[text()='Click me!']
.
If this will return more than one result, IMHO the best way to specify your query is to use css classes. Which ones will most likely not be changed in future is the subject to discuss with your developers team. In most cases a particular element will have several css classes, sometimes they will be even added\modified dynamically. Unfortunately, XPath knows nothing about html/css, it only works with the XML tree. To indicate, that your target element should have a specific class among the others, use the contains()
function. The syntax here may look a bit uncomfortable to use at first, but it's a question of practice. Let's say, your button has a class myBTN. The xpath will look like //button[text()='Click me!' and contains(@class,'myBTN')]
some things to notice - don't miss quotes or brackets. Every string value is always case-sensitive, if you need to check something ignoring the case, you must explicitly use a special function (forgot the name, easy to google if you need). Well that's the very basics, and also a couple of advice's from the following link
Here you can play with various constructions, checking the resuls immediately. And also, If I were you, I'd spend, let's say an hour, to read here - and try the examples given there, maybe with some variations of your choice.
Wish you best of luck!