- Jun 02, 2018
- admin
- 0
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:
1234567 | <form name=”loginForm”>Login Username: <input id=”username”type=”text”name=”login” /> Password: <input id=”password”type=”password”name=”password” /><input type=”submit”name=”signin”value=”SignIn” /></form> |
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:
123 | WebElement elementUsername=driver.findElement(By.id(“username”));WebElement elementPassword=driver.findElement(By.id(“password”)); |
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:
123 | WebElement elementUsername=driver.findElement(By.name(“login”));WebElement elementPassword=driver.findElement(By.name(“password”)); |
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:
1 | <a href=”link.html”>Name of the Link</a> |
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:
1 | WebElement element=driver.findElement(By.linkText(“Name of the Link”)); |
Link Text Locator:
If there are multiple elements with the same link text then the first one will be selected. This Link Text locator works only on links (hyperlinks) so it is called as Link Text locator.
Syntax:
1 | findElement(By<span style=”color: #ff0000;”>.linkText(“LinkText”)</span>) |
123 | <span class=”nodeLabelBox repTarget “><<span class=”nodeTag “>span</span><span class=”nodeAttr editGroup “><span class=”nodeName editable “>id</span>=”<span class=”nodeValue editable”>link-signup</span>”</span><span class=”nodeBracket editable insertBefore “>><<span class=”nodeTag “>a</span><span class=”nodeAttr editGroup “><span class=”nodeName editable “>href</span>=”<span class=”nodeValue editable”>https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F</span>”</span>><span class=”nodeText editable”><span class=” “>Create account</span></span></<span class=”nodeTag “>a</span>></span></span></span> |
Value to be added in the By.linkText method:
1 | findElement(By.<span style=”color: #ff0000;”>linkText(“Create account”)</span>) |
Script:
12345678910111213 | packageseleniumTutorial;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.firefox.FirefoxDriver;publicclassLocators{ publicstaticvoidmain(String[]args){ WebDriver driver=newFirefoxDriver(); driver.get(“<a href=”https://www.gmail.com/” target=”_blank” data-saferedirecturl=”https://www.google.com/url hl=en&q=https://www.gmail.com&source=gmail&ust=1475225889620000&usg=AFQjCNGWiJEPM95P3VkSISyTSrTuSE2t5A”>https://www.gmail.<wbr />com</a>”); driver.findElement(By.linkText(“Create account”)).click(); }} |
Partial Link Text:
In some situations, we may need to find links by a portion of the text in a Link Text element. it contains. In such situations, we use Partial Link Text to locate elements.
Syntax:
1 | findElement(By.partialLinkText(“partialLinkText”)) |
12345 | <span class=”nodeLabelBox repTarget “><<span class=”nodeTag “>span</span><span class=”nodeAttr editGroup “><span class=”nodeName editable “>id</span>=”<span class=”nodeValue editable”>link-signup</span>”</span><span class=”nodeBracket editable insertBefore “>><<span class=”nodeTag “>a</span><span class=”nodeAttr editGroup “><span class=”nodeName editable “>href</span>=”<span class=”nodeValue editable”>https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F</span>”</span>><span class=”nodeText editable”><span class=” “>Create account</span></span></<span class=”nodeTag “>a</span>></span></span></span> |
Value to be added in the By.partialLinkText method:
1 | findElement(By.partialLinkText(“Create”)) |
Script:
12345678910111213 | packageseleniumTutorial;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.firefox.FirefoxDriver;publicclassLocators{ publicstaticvoidmain(String[]args){ WebDriver driver=newFirefoxDriver(); driver.get(“https://www.gmail.com”); driver.findElement(By.partialLinkText(“Create”)).click(); }} |
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] |