I am working on Automation using Selenium Webdriver with Java. For locating elements i use XPath. Is it a good practice?
-
Yes, that's how everyone does it. – Dawood ibn Kareem Apr 14 '16 at 10:41
-
1@DavidWallace No, it isn't. And using `xpath` with absolute path is the worst thing you can do. – Guy Apr 14 '16 at 10:43
-
@Guy then maybe you should post an answer explaining what you do instead. Certainly all the professional testers I've worked with have done this, or something similar to it. – Dawood ibn Kareem Apr 14 '16 at 10:46
-
Possible duplicate of [Locating an element by id](http://stackoverflow.com/questions/34521441/locating-an-element-by-id) – Guy Apr 14 '16 at 10:51
-
@DavidWallace this question as been asked many times ([example](http://stackoverflow.com/questions/34521441/locating-an-element-by-id)). `xpath` is the slowest way to locate element (known fact). Using absolute path make it even worse, not to mention it make it very fragile. – Guy Apr 14 '16 at 10:54
-
I've seen ridiculously long and brittle XPaths with *no semantic value* generated by Firefox etc. and used without question all over this site. That's a maintenance nightmare waiting to happen, irrespective of any minuscule improvement in search-time. In any case, your typical WebDriver developer will waste far more time than he saves, with excessive waits and sleeps. – Andrew Regan Apr 14 '16 at 12:22
3 Answers
Absolute XPath
Absolute XPath starts with the root node or a forward slash (/). The advantage of using absolute is, it identifies the element very fast. Disadvantage here is, if any thing goes wrong or some other tag added in between, then this path will no longer works.
Example: If the Path we defined as
html/head/body/table/tbody/tr/th
Relative Xpath
A relative xpath is one where the path starts from the node of your choise - it doesn't need to start from the root node.
It starts with Double forward slash(//)
Syntax:
//table/tbody/tr/th
Advantage of using relative xpath is, you don't need to mention the long xpath, you can start from the middle or in between.
Disadvantage here is, it will take more time in identifying the element as we specify the partial path not (exact path).

- 16,610
- 15
- 78
- 125
There are no general rules. It always depends on the site under test.
The best way to locate your elements is by Id. This is not always possible, especially when the site under test is not under your control and you cannot add id's to the html yourself. Locating by Name is not bad either, but depending on the site you test, it may not be unique on every page/frame. (Note that "Name" means the name
attribute in HTML, not the tag name!)
For <a>
and <button>
elements, searching by LinkText or PartialLinkText can be a good choice, but only if your site under test cannot be switched to another language, because this usually changes all link texts, too.
Locating by CSS selector is almost as powerful as by XPath but usually much shorter and more compact, especially when dealing with class names.
Example (CSS vs. XPath expressing the exact same thing):
driver.findElement(By.cssSelector(".ecp-cartItemText"));
driver.findElement(By.xpath("//*[@class='ecp-cartItemText' or contains(@class,' ecp-cartItemText ') or substring(@class,1,17)='ecp-cartItemText ' or substring(@class,string-length(@class)-16)=' ecp-cartItemText']"));
Only when you search for text as content of an HTML element, CSS is not able to do it (no XQuery available in Selenium!) and you have to use XPath.
Example:
//button[@type='button']//span[text()='Close']
Also for searching relative to already located web elements along XPath axes, it might be useful to use XPath sometimes.
Example:
element.findElement(By.xpath("following-sibling::p[1]/strong"));
There might be other cases where XPath cannot be avoided, but I haven't encountered any. So use XPath only when necessary! There are usually better ways.

- 3,042
- 1
- 17
- 20
-
Maybe also a bit about the importance of readability / maintainability / stability? – Andrew Regan Apr 14 '16 at 13:10
Your use of the XPath selectors or not depends on how specific you need your document structure to be.
For example, if you want to click the 'submit' button on the form, but don't really care where the submit button is, then referencing an element by its id or name is a good idea and common practice. If, on the otherhand, you are testing for an element in the page that needs to be a sub-element of something else, then using Xpath is perfectly fine.

- 3,197
- 2
- 27
- 46