2

Since we say that IDs are faster and are preferred over XPaths, how different would the execution of the following be?

  1. driver.findElement(By.xpath("//button[@id='Save']")).click();

  2. driver.findElement(By.id("Save")).click();

  • Why dont you try to write couple of code lines to measure it on your own? – Alexey R. Oct 4 '17 at 10:21
  • Yes I could very well do it. But I guess, it again depends on how big the test script is going to be and how good the application is built. – kanayikkaran Oct 4 '17 at 12:37
3

When you are looking for an element by id, this is going to eventually come down to document.getElementById() execution which is blazingly fast and highly optimized by the modern browsers.

XPath location strategy in this case requires traversing the whole HTML tree even though having the id attribute check is usually optimized in XPath engines.

2

It depends what you mean by different. Best practice would be to use a static ID of some kind, either whether a test ID or through an element ID - as long as it's 1) unique and 2) static.

XPath isn't ideal as it's relative to other elements on the page, meaning it's liable to change fairly often.

  • In the example I've mentioned, first one is in xpath format, but ultimately uses an ID. How different would the finding of that element be, than using an ID directly? – kanayikkaran Oct 4 '17 at 12:34
1

ID have high priority over the xpath

priority of locators

id>name>tagname>classname>xpath>css
0

Nothing different as you feel. It is also a type under locators. If you didn't find element locators like id, name, link text etc. Last way you can choose or locate the "Xpath". Better way to find xpath is by using "Firebug and Firepath". It makes to find locators easily.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.