XPath or CSS or ID–Which one to use

XPath or CSS or ID – Which one to use?

One of the most debated topics in the open-source UI test automation community is selecting the right UI locator. Amongst many options available for identifying the UI locators today, such as HTML ID, Name, CSS, and XPath expressions, etc., the most common recommendation is to use HTML ID and the least recommended is XPath. Some of the most common opinions that we come across in discussion forums about XPaths are that:

1. They are brittle and tend to break easily even with minor application changes and thus require regular test automation maintenance
2. They tend to take a relatively long time for execution
3. They are hard to read and understand

Does XPaths take a longer time than other UI locators? If so, then how long? How does it impact the overall test execution time?

To answer these questions, we performed a small exercise.

Our Test Candidate

We picked a test case from a random e-commerce application. The test case had the following attributes:

  • The test case had 13 test steps
  • It had actions like 10 clicks, 6 send keys, and 2 get texts.
  • The respective UI controls for the above actions included HTML IDs along with other HTML attributes such as name, placeholder, type, role, href, data-nav-role, and style.

Test Variations

From the above test case, multiple copies of automated test scripts were created and named as ID Test, CSS referencing ID Test, XPath referencing ID Test, CSS without ID Reference, and XPath without ID reference.

Here are the additional details:

Test 1 – ID Test

All the web elements (13) were located with ID reference as shown below.

     Ex: driver.findElement(By.ID(“txtUsername”))

Test 2 – CSS referencing ID Test

All the web elements (13) were located with ID reference by using cssSelector method as shown below.

     Ex: driver.findElement(By.cssSelector(“#txtUsername”))

Test 3 – Xpath referencing ID Test

All the web elements (13) were located with ID reference by using the Xpath method as shown below.

     Ex: driver.findElement(By.xpath(“//input[@id=’txtUsername’]”));

Test 4 – CSS without ID Reference

All the web elements (13) were located without ID reference by using different types of cssSelector methods as shown below.

    Ex: driver.findElement( By.cssSelector(“div>input[name=”txtUsername”]));

Test 5 – Xpath without ID reference

All the web elements (13) were located without ID reference by using different types of Xpath locators (including the Xpath Axes) method as shown below.

     Ex: driver.findElement( By.xpath(“//div/input[@name=’txtUsername’]”));

Our Test Automation Infrastructure

The above automated test scripts were executed multiple times in the following machine configuration:



A. Using Selenium Version 3.141.59:



Note: The above tests were executed for 40 times on 2 different machines and the averages were taken out to eliminate Internet bandwidth and OS related dependencies

Time in Seconds Per Execution

B. Using Selenium Version 4.0.0-alpha-6

Browser-execution-in seconds

Time in Seconds Per Execution-2


From the tests performed, we observed that:

  • Firefox was relatively slow with the test execution. Surprisingly, Microsoft Edge turned out to be quickest and even better than Google Chrome.
  • The execution time difference between the ID test and XPath/CSS referencing ID Test was minimal, having a difference of only a few milliseconds.
  • Locator types that did not use IDs, took at least 1-2 seconds more than the ID test. This meant a difference of 2-3 minutes if the test suite had about 50-100 automated test scripts.
  • Selenium versions and WebDriver browser versions had no impact on these metrics.

Our Recommendation

1. Use IDs directly by using the By.id method from selenium.
2. If you prefer to use Xpaths/CSS, try to reference IDs in them and do not use //* preceding the locator.
3. Avoid using absolute XPaths/CSS at all costs.

Now, you will have a clear understanding of which locator is better to use when it comes to selecting the right one for your web and mobile testing. Feel free to contact us if you need more details or want to know about our projects, detailed test references, and the various recommendations we have provided to our clients.

By: Venkata Sai Annam, Vamsi Akula, and Amulya Poduri