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:

    1. css selector,
    2. link text selector,
    3. partial link text selector,
    4. tag name,
    5. xpath.

    w3 Webdriver locator strategies

    As you can see there is no "Id". By.Id is sugar syntax added by Selenium and other WebDriver implementations. Under the hood it just translates By.Id to 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.



Suggested Topics