locating element by id or css, which is faster?
If there is an id available for an object in a page, find element by ID or CSS - which is faster?
element(by.id("btnButtonID")) OR element(by.css("#btnButtonID")
Since we can identify any objects using CSS locator, is it a standard practice to use only CSS locator in our page objects? Does it help to look the code readable and cleaner?
Any WebDriver will use Json Wire Protocol to interact with the browser. The protocol defines 5 locator strategies:
- css selector,
- link text selector,
- partial link text selector,
- tag name,
As you can see there is no "Id".
By.Idis sugar syntax added by Selenium and other WebDriver implementations. Under the hood it just translates
By.Idto either css or xpath selector. So to asnwer your question - a css selector using the ID might be slightly faster as you ommit the mapping.