Search On Google

Thursday, February 14, 2019

Locators in selenium

What is locators?

Selenium provides a number of Locators to precisely locate a GUI element.
There are many locators for select particular element .

There are mainly 7 locators:-

a)id
b)name
c)className
d)cssSelector
e)tagName
f)linktext
g)xpath

1.id locator:-

ID locator for get access to particular element . This loccator work with  id of element.
Example:- <input type="text"  id="id_002">
Example in code:- driver.findElement(By.id("id_002"))

2.className locator: -

className is locator for get access to particular element . This loccator work with  className of element.
Example:- <input type="text"  class="id_002">
Example in code:- driver.findElement(By.className("class1"))

3.Name locator:- 

This locator for get access to particular element . This loccator work with  className of element.
Example: - <input type="text"  name="name">
Example in code:- driver.findElement(By.name("name"))

4.HTML tag Name locator:- 

This locator used for get access to particular element by using tag name.
Example: - <input type="text" >
Example in code:- driver.findElement(By.tagName("input"))

5.link Text locator:-

LinkText locator is used for get acess element by using hyperlinks. If there will be many hyperlinks then top first will be selected.
Example:- findElement(By.linkText("LinkText"))

6.CSS selector:-   


TAG and ID:-
findElement(By.cssSelector(tag#id))

TAG and class:-
findElement(By.cssSelector(tag.class))

TAG and attribute:-
findElement(By.cssSelector(tag[attribute=value]))

TAG and class and attribute:-
findElement(By.cssSelector(tag.class[attribute=value]))

Example:- <input type="text" id="id_003"><br>
Example in code:-driver.findElement(By.cssSelector("input#id_003"))

7.Xpath locators:-


This locator is used in xml documents navigation mostly. Xpath stand for XML path.
Example:- Xpath=//tagname[@attribute='value']
Example in code:- findElement(By.xpath("XPath"))

There are two types of xpath absolute and relative.
We mostly use relative xpath because absolute xpath will be much more in length.

Example with html code:-

<html>
 <head></head>
<body> <div><h1>Heading…</h1></div></body>
</html>

Here absolute path for H1 tag will be         html/body/div/h1
Here relative path for h1 is                          //html/body/h1/text()


All locators Example with java code:-


package pkg1;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class MyClass4_locators {
@SuppressWarnings("static-access")
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String baseUrl = "https://www.onlinetutorial.info/p/locators-example.html";
    driver.get(baseUrl); 
    
    
     WebElement  email = driver.findElement(By.name("myname"));  
     email.sendKeys("pradeepyadav"); 
    
        driver.findElement(By.id("id_002")).sendKeys("sandeep yadav");  
    driver.findElement(By.className("class1")).sendKeys("rahul yadav"); 
    driver.findElement(By.tagName("input")).sendKeys("rohit");
        
    driver.findElement(By.cssSelector("input#id_003")).sendKeys("A");
    driver.findElement(By.cssSelector("input.class2")).sendKeys("B");
    driver.findElement(By.cssSelector("input#awe[type=text]")).sendKeys("C");
    driver.findElement(By.cssSelector("input.class3[type=email]")).sendKeys("D");
 
    
}

}

No comments:

Post a Comment

About Me

My photo
Mumbai, Maharashtra, India