Of the many findElement(s)/By functions in Selenium, when would you use one over the other?

This question have been asked and answered in numerous forums in different formats. Considering them all if we prioritize the locators the list would be as follows :

  • id: Select element with the specified id attribute.
  • name: Select first element with the specified name attribute.
  • link_text: Select link (anchor tag) element which contains text matching the specified LinkText.
  • partial_link_text: Select link (anchor tag) element which contains text matching the specified PartialLinkText.
  • tag_name: Locate Element using a Tag Name.
  • class_name: Locate Element using a ClassName.
  • css_selector: Select the element using CssSelectors.
  • xpath: Locate an element using an XPath expression.

So the question now is Whats New?

The answer is Selenium have evolved a lot recently. WebDriver is now a W3C Recommendation Candidate. Things within Selenium are changing pretty fast. It’s no more only about choosing the locator. We need to use a locator which will :

  • Uniquely identify an element.
  • The performance of the locator must be optimized.

Keeping these two factors in mind, the best strategy would be to Mock the DOM. The W3C Recommendation Candidate does mentions the list of the locators as per the below :

Selenium_Locators

So the verdict is clear and concise.

Leave a Comment