Locators

Locators-These are known as identifiers. Selenium WebDriver uses different locators to find the elements on web page.

The different types of locator are:

  • ID

  • Name

  • Link Text

  • CSS Selector

  • DOM (Document Object Model)

  • XPath

ID

This is the most common way of locating elements since ID's are supposed to be unique for each element. Target Format: id=id of the element Example: Id=email

Name

Locating elements by name are very similar to locating by ID.But multiple element can have the same name property. When element have the same property it always identify very first element with that property. Target Format: name=name of the element Example: Name=username Locating by Name using Filters Filters can be used when multiple elements have the same name. Filters are additional attributes used to distinguish elements with the same name. Target Format: name=name_of_the_element filter=value_of_filter . Example: name=tripType value=oneway

Link Text- This type of locator applies only to hyperlink texts.

Target Format: link=link_text Example : Link=REGISTER

CssSelector- CssSelector is the most common locating strategy of advanced Selenium users because it can access even those elements that have no ID or name.

It is used to identify an element based on a combination of HTML tag, id, class, and attributes.

CSS Selectors have many formats like:

  • Tag and ID

  • Tag and class

  • Tag and attribute

  • Tag, class, and attribute

  • Inner text

Locating by CSS Selector - Tag and ID

Target Format: css=tag#id tag- the HTML tag #- This should always be present when using a CSS Selector with ID id- the ID of the element Example- css=input#email

Locating by DOM (Document Object Model)

There are four basic ways to locate an element through DOM:

  • getElementById

  • getElementsByName

  • dom:name (applies only to elements within a named form)

  • dom:index

Locating by DOM – getElementById

Target Format: document.getElementById("id of the element") Example-document.getElementById("persist_box")

Locating by DOM - getElementsByName

Target Format: document.getElementsByName("name")[index] Example-document.getElementsByName("servClass")[0]

Locating by DOM - dom:name

Target Format: document.forms["name of the form"].elements["name of the element"] Example-document.forms["home"].elements["userName"]

Locating by DOM - dom:index

Target Format: document.forms[index of the form].elements[index of the element] Example- document.forms[0].elements[3] (Phone No Field) Example-document.forms[0].elements["phone"]

Last updated