selenium locators tutorial

What is locator and what is Element ? How to work with locators in Selenium ?

In selenium we have 8 types of locators are avalable. 

  1. ID
  2. Name
  3. CSS
  4. XPath
  5. Classname
  6. TagName
  7. LinkText
  8. Partial Link Text

Ex : ID

package locators;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;

public class SeleniumLocators {
    WebDriver driver;
 
@Test(description= "find the id locator using firebug or inspect element" )
  public void id() {
      driver.get("http://www.seleniumlearn.com/java");
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
      driver.findElement(By.id("edit-search-block-form--2")).sendKeys("selenium");
      driver.findElement(By.id("edit-search-block-form--2")).sendKeys(Keys.ENTER);
      
  }

  @BeforeTest
  public void beforeTest() {
      driver=new FirefoxDriver();
     // System.setProperty("webdriver.chrome.driver","D:\\lib\\chromedriver.exe"); //---> Chromedriver path
     // driver = new ChromeDriver();
      driver.manage().window().maximize();
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
  }

}

Ex : Name

package locators;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;

public class SeleniumLocators {
    WebDriver driver;
 
    @Test
    public void name() {
          driver.get("http://www.seleniumlearn.com/user");
          driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
          driver.findElement(By.name("name")).sendKeys("purushotham");
         
    }

  @BeforeTest
  public void beforeTest() {
      //driver=new FirefoxDriver();
System.setProperty("webdriver.chrome.driver","D:\\lib\\chromedriver.exe"); //---> Chromedriver path
driver = new ChromeDriver();
driver.manage().window().maximize();
        }

}

Locator : className 

package locators;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;

public class SeleniumLocators {
    WebDriver driver;
 
    @Test
    public void className() {
          driver.get("http://www.seleniumlearn.com/user");
          
          driver.findElement(By.className("form-submit")).click();
         
    }

  @BeforeTest
  public void beforeTest() {
     driver=new FirefoxDriver();
//System.setProperty("webdriver.chrome.driver","D:\\lib\\chromedriver.exe"); //---> Chromedriver path
//driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        }

}

Locator : cssSelector

package locators;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;

public class SeleniumLocators {
    WebDriver driver;
 
    @Test
    public void className() {
          driver.get("http://www.seleniumlearn.com/user");
          driver.findElement(By.cssSelector("input.form-submit")).click();
     }

  @BeforeTest
  public void beforeTest() {
 driver=new FirefoxDriver();
//System.setProperty("webdriver.chrome.driver","D:\\lib\\chromedriver.exe"); //---> Chromedriver path
//driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        }

}

Locator : linkText

package locators;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;

public class SeleniumLocators {
    WebDriver driver;
 
    @Test
    public void linkText() {
          driver.get("http://www.seleniumlearn.com");
          
          driver.findElement(By.linkText("Quiz")).click();
         
    }

  @BeforeTest
  public void beforeTest() {
 //driver=new FirefoxDriver();
System.setProperty("webdriver.chrome.driver","D:\\lib\\chromedriver.exe"); //---> Chromedriver path
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        }

}

Locator : partialLinkText

 

package locators;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;

public class SeleniumLocators {
    WebDriver driver;
 
    @Test
    public void particalLinkText() {
          driver.get("http://www.seleniumlearn.com");
          
          driver.findElement(By.partialLinkText("Code")).click();
         
    }

  @BeforeTest
  public void beforeTest() {
//driver=new FirefoxDriver();
System.setProperty("webdriver.chrome.driver","D:\\lib\\chromedriver.exe"); //---> Chromedriver path
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        }

}

Locator : tagName

package SeleniumLearn.Com;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;

public class TotalNumberLinks{
     public WebDriver driver;
@Test 
       public void TotalNumberofLinks() throws InterruptedException {
        driver.get("http://www.seleniumlearn.com");
        driver.manage().timeouts().implicitlyWait(9, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        List<WebElement> list=driver.findElements(By.tagName("a"));
        System.out.println("Total number of links:"+ list.size());

         }

@BeforeTest
  public void beforeTest() {
      driver= new FirefoxDriver();
      driver.manage().window().maximize();
  }

@AfterTest
  public void afterTest() {
  }
}

XPATH :-

 

Comments