Most of the Automated UI Selenium WebDriver test are very slow comparing to Unit Tests and API tests and one of the factors is how fast WebDriver can find elements within the HTML of a web page.
Also, using the right locator ensures your tests are faster, more reliable and have a lower maintenance over releases.
Finding web elements with Selenium WebDriver by ID is usually the fastest option, but here is the list of the best and fastest selectors Selenium WebDriver Tests run faster:
- ID selectors (By.ID – Matches by @id attribute)
- IDs are the safest, fastest locator option and should always be your first choice
- IDs should be unique in every page according to W3C website
- even if the DOM changes, if the ID is still there, then WebDriver can still locate it
- always try and get extra IDs added into the code, this makes testers life easier
- CSS and Name selectors (Matches by CSS selector or @name attribute)
See more details
- faster than XPath
- whenever IDs are not available/ usable, usually @name can be used to identify elements
- CSS and XPath locators are very similar
- not very flexible in identifying elements comparing to XPath
- XPath locators (Matches with arbitrary XPath expression)
- most flexible in order to build reliable web element locators
- very slow locator (particularly in IE) since in order to locate the element it needs to traverse the whole DOM of the page which is a time consuming operation
- check here how to identify and validate XPath locators inside Chrome Developer Tool, also you can install a Firebug extension to be able to identify and validate XPaths easily with FirePath from FireBug (adds a development tool to edit, inspect and generate XPath 1.0 expressions, CSS 3 selectors and JQuery selectors)
LoadFocus.com is a All-In-One Cloud Testing Platform for Load Testing and Performance Testing, Website Speed Testing, Automated Website Testing and Mobile Emulation for Websites, Mobile Applications and API Endpoints.