Locators in Selenium



Locators in Selenium

 

What are Locators?

Locator is a command that tells Selenium IDE which GUI elements ( say Text Box, Buttons, Check Boxes etc) its needs to operate on.  Identification of correct GUI elements is a prerequisite to creating an automation script. But accurate identification of GUI elements is more difficult than it sounds. Sometimes, you end up working with incorrect GUI elements or no elements at all!  Hence, Selenium provides a number of Locators to precisely locate a GUI element

The different types of Locators in Selenium IDE

This tutorial explains different locators, how, when and ideal Strategies to use these locators.

Different types of Locators

Here are some of the locators: id, name, identifier, dom, xpath, link, css and ui that Selenium’s commands support. I will explain just a few most important locators to avoid over dosage and any confusion.

  •  ID
  •  Name
  • Tag and ID
  • Tag and class
  • Tag and attribute
  • Tag, class, and attribute
  • Link Text
  • CSS Selector
  • Inner text
  • DOM (Document Object Model)
  • XPath

ID Locator

Ids are the most preferred way to locate elements on a page, as each id is supposed to be unique which makes ids a very faster and reliable way to locate elements.

With this strategy, the first element with the id attribute value matching the location will be returned. If no element has a matching id attribute, a NoSuchElementException will be raised.

Example: If an element is given like this:

Login Username:  Password:  

You can easily choose the element with the help of ID locator from the above example:

id = “username”

id = “password”

Use the same in your Selenium test script as well:

Even though this is a great locator, obviously it is not realistic for all objects on a page to have ids. In some cases developers make it having non-unique ids on a page or auto-generate the ids, in both cases it should be avoided.

 

Name Locator

This is also an efficient way to locate an element  with name attribute, after Ids give it your second preference but likewise Ids, name attributes don’t have to be unique in a page.

With this strategy, the first element with the name attribute value matching the location will be returned. If no element has a matching name attribute, a  NoSuchElementException will be raised.

Example: Let’s take the above example:

You can easily choose the element with the help of Name locator from the above example:

name = “login”

name = “password”

Use the same in your Selenium test script as well:

 

Identifier Locator

This selects the element with the specified @id attribute. If no match is found, then it tries to select the first element whose @name attribute is id.

Example: Valid locator for above example is:

identifier = “password”

First it will try to find the locator with id = “password” if it exists, otherwise it would target name = “password”.

 

Link Locator

With this you can find elements of “a” tags(Link) with the link names. Use this when you know link text used within an anchor tag.

Example: If an element is given like this:

To click this hyperlink using the anchor tag’s text, you can use the link locator:

link=”Name of the Link”

Use the same in your Selenium test script as well:

DOM Locator

The DOM strategy works by locating elements that matches the JavaScript expression referring to an element in the DOM of the page. DOM stands for Document Object Model. DOM is convention for representing objects in HTML documents.

Example: To select the username from the above example you can use the following ways: document.forms[0].elements[0]

document.forms[‘loginForm’].elements[‘username’]

document.forms[‘loginForm’].username

document.getElementById(‘username’)

 

XPath Locator

While DOM is the recognized standard for navigation through an HTML element tree, XPath is the standard navigation tool for XML; and an HTML document is also an XML document (xHTML). XPath is used everywhere where there is XML.

Example: To select the username from the above example you can use the following ways:

xpath=//*[@id=’username’]

xpath=//input[@id=’username’]

xpath=//form[@name=’loginForm’]/input[1]

xpath=//*[@name=’loginForm’]/input[1]

Summary

Syntax for Locator Usage

Method

Target Syntax

Example

By ID id= id_of_the_element id=email
By Name name=name_of_the_element name=userName
By Name Using Filters name=name_of_the_element filter=value_of_filter name=tripType value=oneway
By Link Text link=link_text link=REGISTER
Tag and ID css=tag#id css=input#email
Tag and Class css=tag.class css=input.inputtext
Tag and Attribute css=tag[attribute=value] css=input[name=lastName]
Tag, Class, and Attribute css=tag.class[attribute=value] css=input.inputtext[tabindex=1]
Tags: ,
Leave a comment

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

Subscribe now

Receive weekly newsletter with educational materials, new courses, most popular posts, popular books and much more!

https://bridgejunks.com/ https://crownmakesense.com/ https://brithaniabookjudges.com/ https://hughesroyality.com/ https://rhythmholic.com/ https://bandar89.simnasfikpunhas.com/ https://www.100calshop.co.il/products/thailand/ https://myasociados.com/ https://solyser.com/ http://konfidence.cz/ https://muscadinepdx.com/ https://bandar89.parajesandinos.com.ve/ https://goremekoop.com/ https://oncoswisscenter.com/ https://www.turunclifehotel.com/bandar89/ https://www.houseofproducts.biz/ https://taimoormphotography.com/
BIJI18 BIJI18 BIJI18