Locators

0 votes

Locators are the way to tell Selenium WebDriver with which web element we like to do something (It can be literally anything, like: `clicking`, `typing`, `selecting`, `verifying`, and so on).

Selenium WebDriver provides different ways of locating those web elements; we call this the locating strategy. In Selenium WebDriver we can locate web elements by CSS, XPath, Name, Id, and even Link text. We have to define the locators our self, so we have full control over it.

The CSS locating strategy is recommended for three reasons:

  1. It’s faster;
  2. It’s more readable;
  3. It’s more used.

It depends on the browser, but in Internet Explorer the XPath locators are 2 (up to 3) times slower than CSS locators. XPath is implemented differently amongst browsers. CSS locators require very low maintenance, because the syntax is readable and understandable. Beside of that, CSS locators are more used in projects, for example by designers or jQuery experts (which is a frequently used JavaScript library).

It becomes unnecessary hard to interact with elements on the page if there are no unique identifiers. So, try to convince the developers to add some handy identifiers.

General advice might be to use the locator strategies in the following order: id > name > CSS > XPath > angular. Only use XPath if we need to walk up the DOM.