Which Selenium WebDriver locator is faster?

Which Selenium WebDriver locator is faster?

According to this article on Medium: "Which locator is faster in identifying elements in Selenium?", the order of Selenium Locators (fast to slow) is "ID, Name, CSS, XPath".

Today I did a benchmark test to compare those 4 Selenium locators, I got quite different results. As we know, the time took to identify one web element is very much dependent on the page content. There are many factors that affect the results, such as the size and structure of HTML, the number of elements, ..., etc. In other words, people get different results with different test data.

I planned to be as objective and realistic as possible.

  • Use the simplest form possible

The purpose is to test the raw locator speed. If using complex XPath expressions including XPath functions such as "contains(text(), 'SPECIAL TEXT')", the result for XPath will be slow.

  • Realistic web pages

I used two common pages: Sign Up (simple) and Preferences (medium with many form fields) on a modern web site based on the Material design.

No alt text provided for this image
No alt text provided for this image


Test Methods

  1. Run tests against a local WhenWise test server, eliminating any network issues
  2. Run a set of selenium steps (using the same locator) repeatedly for a fixed time, then check how many loops it completed.

Test Scripts (locator part):

by ID:

driver.find_element(:id, "search")
driver.find_element(:id, "modules_appointment_checkbox")
driver.find_element(:id, "terminology_human_resource")
driver.find_element(:id, "booking_interval_between_bookings")
driver.find_element(:id, "booking_max_client_distance")
driver.find_element(:id, "constraints_member_min_time_allowed_for_client_cancellation")
driver.find_element(:id, "save_preferences_button")

by Name:

driver.find_element(:name, "search")
driver.find_element(:name, "modules[appointment]")
driver.find_element(:name, "terminology[human_resource]")
driver.find_element(:name, "booking[interval_between_bookings]")
driver.find_element(:name, "booking[max_client_distance]")
driver.find_element(:name, "constraints[member_min_time_allowed_for_client_cancellation]")
driver.find_element(:name, "commit")

 by XPath:

driver.find_element(:xpath, "https://input[@type='search']")
driver.find_element(:xpath, "https://input[@type='checkbox' and @name='modules[appointment]']")
driver.find_element(:xpath, "https://input[@type='text' and @name='terminology[human_resource]']")
driver.find_element(:xpath, "https://select[@name='booking[interval_between_bookings]']")
driver.find_element(:xpath, "https://input[@type='number' and @name='booking[max_client_distance]']")
driver.find_element(:xpath, "https://input[@type='number' and @name='constraints[member_min_time_allowed_for_client_cancellation]']")
driver.find_element(:xpath, "https://input[@type='submit' and @value='Save']")

by CSS:

driver.find_element(:css, "input#search")
driver.find_element(:css, "input#modules_appointment_checkbox")
driver.find_element(:css, "input#terminology_human_resource")
driver.find_element(:css, "select#booking_interval_between_bookings")
driver.find_element(:css, "input#booking_max_client_distance")
driver.find_element(:css, "input#constraints_member_min_time_allowed_for_client_cancellation")
driver.find_element(:css, "input#save_preferences_button")

Test Results:

Single Run of 6 locator steps:

ID:     39 ms
Name:   38 ms
XPath:  40 ms
CSS:    36 ms

5 seconds of running five locator steps against WhenWise's SignUp Page:

ID:    346 loops
Name:  390 loops
XPath: 384 loops
CSS:   396 loops

8 seconds of running six locator steps against WhenWise's Business Preferences Page:

ID:    392 loops
Name:  430 loops
XPath: 419 loops
CSS:   454 loops

Verdict:

  • All core locators are quite fast
  • The raw speed differences, in the simplest locator expression form, are minor
  • The raw speed order (fast to slow): CSS, NAME, XPath, ID

Analysis:

  • ID locator is the slowest. This surprised me. One explanation I can get to is that I added many ID attributes to web elements (which is a fairly common practice).
  • CSS locator is the fastest, I think this is due to Chrome's optimization for rendering.
  • XPath locator performs a lot better than I expected, maybe due to the simplest form of XPath expression used, and Chrome might have optimizations on that.

Recommendations:

  1. Use the most reliable locator that is less prone to change for your app. Generally speaking, ID is a better choice, despite it may be slower for some pages.
  2. Prefer a more readable form if possible. For example, "find_element(:xpath, '//input[@value='Sign in'])" is better than "find_element(:name, 'submit')"

Test setup: MacBook Pro 16", Chrome 87, Selenium WebDriver 3.142.7 with Ruby 3.0. Test scripts were developed in TestWise IDE and the results from executions in a terminal window.


Ahmed Elsharkawy

QA Automation Engineer Backend GraphQL, playwright, C#, Java, postman

2 年

always I like to locate elements using XPath it is so interesting, I know XPath is faster than CSS but it is super good to know now this info about id, sometimes I locate elements using Xpath with ID Attribute and also CSS with ID Attribute and it is so public using protractor

回复

要查看或添加评论,请登录

Zhimin Zhan的更多文章

社区洞察

其他会员也浏览了