Selenium Web Driver Selectors/Locators

By YuwebDesign

Locators/Selectors in Selenium Web Driver

Public abstract class By in Selenium Web Driver

Mechanism used to locate elements within a document.

Selenium Class By javadoc lists and describes all the locators and location methods.

In order to create your own locating mechanisms,
it is possible to subclass this class and override the protected methods as required,
though it is expected that all subclasses rely on the basic finding mechanisms
provided through static methods of this class:

public WebElement findElement(WebDriver driver) { 
    WebElement element = driver.findElement(By.id(getSelector())); 
    if (element == null) element = driver.findElement(By.name(getSelector()); 
    return element; 
  1. By.ByClassName,
  2. By.ByCssSelector,
  3. By.ById
  4. By.ByLinkText
  5. By.ByName
  6. By.ByPartialLinkText
  7. By.ByTagName
  8. By.ByXPath
  9. ByAll
  10. ByChained
  11. ByIdOrName

There should be only one element with the same id (per W3C, id’s are supposed to be unique on a page). This makes id’s the most reliable locator.

Why choose the id selectors/locators
  1. ID locators are the fastest out of all locators.
  2. ID locators are the safest out of all locators.
    You can rely on your id locator in the future, it will hardly require any efforts for maintenance.

    Your xpath might be working fine now, but if the developer changes the structure of the web page,
    xpath (especially absolute) can stop working unless it involved very smart coding.

    However, id’s do not depend on the page structure,
    they will only stop working if the developer removes this particular id attribute from this particular html element,
    which is a very unlikely scenario.

  3. Syntax:



Id in HTML

In .html document, id attribute is attached to a single HTML element the value of which is unique within the HTML document.
Read more about HTML id attribute.

Id in CSS

In CSS, the #id selector styles the element with the specified id.
Read more about CSS #id Selector

JavaScript HTML DOM getElementById() Method

You might also be interested in JavaScript HTML DOM getElementById() Method.

There can be multiple elements with the same class.

Class Name locator/selector returns the first the element that matches the specified class name.




Class in HTML

In .html document, class attribute is attached to HTML elements.
Read more about HTML class Attribute

Class in CSS

In CSS, class is a CSS selector that is used to style elements it is attached to.
Read more about CSS .class Selector.

JavaScript HTML DOM getElementsByClassName() Method

You might also be interested in JavaScript HTML DOM getElementsByClassName() Method

The getElementsByClassName() method returns a collection of all elements (nodes) in the document with the specified class name, as a NodeList object. The nodes can be accessed by index numbers.
To search for multiple class names, separate them with spaces, like “class1 class2″.

Leave a Reply or Comment

Your email address will not be published. Required fields are marked *