1

A web element that has a unique id/name can be identified using xpath as well. Then what is the advantage in using By.id/By.name over By.xpath. Consider the following example. Below is a web element.

<input id="abc" name="abc" xpath="abc">

This web element can be identified as:

  1. By.id("abc");
  2. By.name("abc");
  3. By.xpath(".//*[@id='abc']); or By.xpath(".//*[@name='abc']);

I want to know what is the advantage of using 1. or 2. over 3.?

NarendraR
  • 7,577
  • 10
  • 44
  • 82
Akash Nigam
  • 300
  • 2
  • 5
  • 11

3 Answers3

4

As Gilles Quenot mentioned, you cannot assume that an ID attribute is unique, but it was and is meant to be, unless we're talking about mobile apps. In web app, ID is the ideal search method because it is supposed to be unique.

The name attribute is often also unique, but it is not necessarily expected to be, and often is not.

One disadvantage of using xpath is that an element could be moved in the document, or other items added, and it might break your xpath depending on how it is crafted. In theory, at least, a locator that uses ID would be "more" immune to such breakage, but as was hinted at, nothing is guaranteed.

As a rule, I will use xpath if a unique ID is not available, and I attempt to write it such that it is generic enough to resist "breakage."

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
Bill Hileman
  • 2,798
  • 2
  • 17
  • 24
  • What if I write xpath such that it is immune to code changes like I mentioned above: By.xpath(".//[@id='abc']); What I wanted to know is - Is using id/name faster than using xpath? – Akash Nigam Oct 02 '18 at 20:40
  • 1
    I've not done benchmarks, but someone I work with who was obsessed with ID being superior to xpath told me he had, and it was faster. I had no reason to doubt him, but that same person also had bad coding practices that made the code run slow in many other ways, so I took it with a grain of salt. Personally I gravitate towards xpath because it's "universal." I don't know if that's a good explanation or not, but I feel I have greater control with xpath. I do, however, use ID when using page object model with `@FindBy` mostly for readabiity whenever I can. – Bill Hileman Oct 02 '18 at 21:23
2

Your question is very valid and every possible option you mention will work but why we have preference of ID and Name locators over XPath?

Instead of giving direct answer Let me explain few other concept which will help you to understand it.

  • The way selenium interacts with web controls through HTML DOM and HTML DOM is W3C standard which is followed by every browser and it's Java script engine such as V8 for chrome, Chakra for Edge etc.

  • HTML DOM expose method for GetElementById() , GetElementsByName() which are all supported by all browser as per W3C standard. So when you use locator such as Id, Name and others which are already implemented and will always work for you.

HTML DOM Methods

Refer https://www.w3schools.com/jsref/dom_obj_document.asp

  • XPath is actually used for navigating through elements and attributes in an XML document. Since is HTML DOM is also form of XML then Xpath will also work but XPath was not designed for HTML DOM. It has other purpose.

  • Conclusion is when you have already methods supported by HTML DOM then preference should be given to those over XPath which is generic way to navigate and There was some instances of XPath with Internet Explorer browser where it was not behaving as expected.

Nitin Sahu
  • 611
  • 6
  • 12
1

I don't think Xpath is less preferred then ID and Name.

  1. ID might not be unique always, in current websites DOM, IDs are mostly changes by time. You cant stay with one ID and run the script again, if you need to try you can check with Amazon Site as well.
  2. Name may be or may not be unique. You are right in some pages you might find elements might have unique names but let's take an example if you have a page with first name and last name sometime dev team just copy and paste those first name lines to last name as well. So now you will find same name attribute in a single page. So you can't rely on name as well.
  3. Xpath is unique, because you are the one who is designing the xpath and and you always want an element with unique identity. So now in Name example you can get the first name and last name separately ny xpath.

I believe Xpath is better choice over the Name and ID attribute.

promitheus
  • 67
  • 2
  • 13