Saturday, 3 March 2018

Published March 03, 2018 by

An Overview on Locators

 An Overview on Locators

In this post, we’ll learn what is Locators and types of Locators.

What is Locators ?

Locators are used to identify the element. 
In Selenium before performing any action (click, type etc) we should find the element using locators. In Selenium there are 8 types are locators. All of them are static methods in By class (it is an abstract class).
  • All the methods takes string as argument and it returns an object of type By.
  • The By object is used as input argument for findElement() method.
  • Return type of findElement() method is WebElement (it is an Interface).
The list of Selenium Locators:
  1. By.tagName
  2. By.id
  3. By.name
  4. By.className
  5. By.linkText
  6. By.partialLinkText
  7. By.cssSelector
  8. By.xpath
Code: Selenium code to click on a link using ‘tagName’:



HTML Code:- 

<html> 
         <body> 
                     <a href=http://localhose id=”a1” name =”n1” class=”c1”>actitime</a>
         </body> 
</html>

- using tagName

driver.findElement(By.tagName("a")).click();
In the browser find the element by tag name ‘a’ and click on it.


import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

public class Demo

{

public static void main(String[] args)

{

WebDriver driver = new FirefoxDriver();
driver.get("file:///D:///Demo.html");
driver.findElement(By.tagName("a")).click();

}

                      }


- using id

driver.findElement( By.id("a1")).click(); 
In the browser find the element by id ‘a1’ and click on it.


import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

public class Demo

{

public static void main(String[] args)

{

WebDriver driver = new FirefoxDriver();
driver.get("file:///D:///Demo.html");
driver.findElement(By.id("a1")).click();

}

                      }

- using name

driver.findElement(By.name("n1")).click(); 
In the browser find the element by name ‘n1’ and click on it.


import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

public class Demo

{

public static void main(String[] args)

{

WebDriver driver = new FirefoxDriver();
driver.get("file:///D:///Demo.html");
driver.findElement(By.name("n1")).click();

}

                      }

- using class

driver.findElement(By.className("c1")).click(); 
In the browser find the element by className ‘c1’ and click on it.


import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

public class Demo

{

public static void main(String[] args)

{

WebDriver driver = new FirefoxDriver();
driver.get("file:///D:///Demo.html");
driver.findElement(By.className("c1")).click();

}

                      }

- using linkText

driver.findElement( By.linkText("actitime")).click(); 
In the browser find the element by linkText ‘actitime’ and click on it.


import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

public class Demo

{

public static void main(String[] args)

{

WebDriver driver = new FirefoxDriver();
driver.get("file:///D:///Demo.html");
driver.findElement(By.linkText("actitime")).click();

}

                      }

- using partialLinkText

driver.findElement(By.partialLinkText("acti")).click(); 
In the browser find the element by partialLinkText ‘acti’ and click on it.


import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

public class Demo

{

public static void main(String[] args)

{

WebDriver driver = new FirefoxDriver();
driver.get("file:///D:///Demo.html");
driver.findElement(By.partialLinkText("acti")).click();

}

                      }



Important Note:

  • If specified locator is matching with more than one element then findElement() method returns the address of first matching element.
  • If the specified locator is not matching with any of the element then findElement() method will throw ‘NoSuchElementException’.