Locators in Selenium
Locators in Selenium
Having understood this, let’s dive deeper and understand the various types
of locators in Selenium.
Now, let’s understand the working of ID locator with the help of an example.
I will launch Google Chrome and navigate to yahoo.com. Here, I will try to
locate the email text box using ID Locator.
On inspecting the above web element, you can see it has an input tag and
attributes like class and id. Now, I will use the value of Id locator i.e. login-
username to locate the email text box.
Let’s see how to automate the text box and send values to it using Id locator.
package Ank;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
public class Locators {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:Selenium-java-
ankchromedriver_win32chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("<a
href="https://login.yahoo.com/">https://login.yahoo.com/</a>");
driver.findElement(By.id("login-username")).sendKeys("[email protected]");
//id locator for text box
WebElement searchIcon = driver.findElement(By.id("login-signin"));//id
locator for next button
searchIcon.click();
}
When you run the above Java program, chrome driver will launch Google
Chrome, redirect to yahoo mail, enter the email address and navigate to next
page. You can refer below image for the output:
Name locator
This is also an effective way to locate an element with a name attribute. With
this strategy, the first element with the value of the name attribute will be
returned. If no element has a matching name attribute, then
a NoSuchElementException will be raised.
Now, let’s see the working of name locator with the help of an example. In
the below image you can see, the name locator possesses a value called
username. The difference here is that you should use a name instead of id.
Let’s take a look at the below code to automate the text box.
1driver.get("<a
2href="https://login.yahoo.com/">https://login.yahoo.com/</a>");
3driver.findElement(By.name("username")).sendKeys("[email protected]");
4//name locator for text box
WebElement searchIcon = driver.findElement(By.name("signin"));//name
locator for next button
searchIcon.click();
In the above code, I have given the value of name locator for the next button
as signin, because when you inspect on that button, you can see in the below
figure its value is signin.
When you run the above Java code, your output will be the same as Id locator.
Now, let’s understand one more locator i.e. linkText.
linkText
You can identify the hyperlinks on a web page using linkText. It can be
determined with the help of an anchor tag (<a>). In order to create the
hyperlinks on a web page, you can use anchor tags followed by the linkText.
Now, let’s see the working of the linkText locator with the help of an example.
Suppose you want to locate ‘Trouble Signing in?‘ link as shown in the below
figure. How will you do that?
In some situations, you may need to find links by a portion of the text in
a linkText element. In such situations, you can use Partial Link Text to locate
elements. Let’s take the same example and try to locate it. I will
choose “Trouble signing in?” link. Now, instead of pasting full text I will just
give it as Trouble. So I will modify my code like this –
driver.get("<a
1href="https://login.yahoo.com/">https://login.yahoo.com/</a>");
2driver.findElement(By.partiallinkText("Trouble")).click();//partiallinkText
locator for links
Now, when you run the above code, it will be redirected to “Trouble Signing
In?” page, but the difference is that you are using partial value to locate the
links. I hope this gives you a clear understanding of how linkText and
partialLinkText locator in selenium works. Now let’s move further and
understand one of the simplest locators in selenium.
CSS Selectors
The CSS is mainly used to provide style rules for the web pages and you can
use it for identifying one or more elements in the web page. The CSS selector
is always the best possible way to locate complex elements in the page.
Let’s take the same example
of the yahoo login page and use CSS selectors. I will inspect the email text
box element. CSS Selector always starts with # and on entering the value
of Id attribute as login-username, the element gets highlighted. Which
implies it was able to locate the element using CSS selector. Now, let’s copy
this value and use the CSS selector in eclipse to understand its working. So,
my code goes like this-
1 driver.findElement(By.cssSelector("#login-
2 username")).sendKeys("[email protected]");
driver.findElement(By.cssSelector("#login-
signin")).click();
When you run the above piece of code, you will be redirected to Yahoo Login
page and it asks you to enter the password. That’s how it works. Now let’s
dive into the last locator of this article and understand XPath.
XPath
XPath is a language to query XML documents. XPath is an important strategy
to locate elements in selenium. It also consists of a path expression along
with some conditions. Here, you can easily write an XPath script/query to
locate any element in the webpage.
Now, let’s try understanding this with the help of an example. I will start
Google Chrome and navigate to google.com. Here, I will try to locate the
search box using XPath. On inspecting the web element you can see it has an
input tag and attributes like class and id. Next, I will make use of tag name
and these attributes to construct XPath which in turn will locate the search
bar.
You just need to click Elements tab and press Ctrl + F to open a search box
in Chrome’s developer’s tool. Here, you can write XPath, string selector and
it gives the search based on that criteria.
In the above image, you can see that it has an input tag. Now, I will start
with // input. Here //input implies a tag name. I will make use of
the name attribute and pass ‘q’ in single quotes as its value. This will give the
below XPath expression:
//input[@name=’q’]
In the above image, you can see that on writing the XPath it has highlighted
the element which implies that this particular element was located using
XPath.
Now, you can use the below code in your Eclipse to locate a particular element
using XPath.
driver.get("<a
href="https://www.google.com/">https://www.google.com/</a>");
1driver.findElement(By.xpath("//input[@id='q']")).sendKeys("Selenium");
2//xpath for search box
3WebElement searchIcon =
driver.findElement(By.xpath("//input[@id='Google Search']"));//xpath
for search button
On writing this code, it will give you an automated search of Selenium.
Basically, this is how you can use XPath.
So, this was all about different types of Locators in Selenium. Now let’s move
further and understand some of the best practices of
using Selenium Locators.
Selenium Locators Best Practices
Understanding the concept of locators in Selenium is one thing, but knowing
how to use them is quite another. Being able to build a robust locator begins
with an understanding of what a robust locator is. Here are three below listed
criteria that you must adhere to while using locators in selenium:
• Robust locators in Selenium are as simple and small as
possible: The more elements a locator contains, the higher the chances
it’ll break because of the change in the page structure.
• Selenium locators still work after you change the properties of a
UI element: Relying on frequently-changed attributes like modifier
classes (menu__item–red) is never a good practice.
• Selenium locators that are robust in nature still work after you
change the UI elements around the element you target: Whenever
you use a non-unique attribute, there are chances that locators will break
because someone added an element with that same attribute above.