Selenium Locators - Id, Name, LinksText, partial LinkText & CSS Selector



Locators in Selenium are one of the most powerful commands. Its ideally the building block of the Selenium automation scripts. It helps locate the GUI elements through which multiple user actions can be performed. These are one of the important parameters for scripting, and if they end up to be incorrect or brittle, they may lead to script failure. A good scripting base foundation requires elements to be located appropriately. For this, we have multiple locators in Selenium WebDriver. Below is the list of these locators of Selenium WebDriver :
  • Id
  • Name
  • Link Text
  • Partial LinkText
  • ClassName
  • Tag Name
  • CSS Selector
  • XPath



Example 1:
---------------------------------------------
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class LocatorsDemo1 {

public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver","C:\\Drivers\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("http://automationpractice.com/index.php");

driver.manage().window().maximize(); // maximize web page


// id & name locators
WebElement searchbox=driver.findElement(By.id("search_query_top"));
searchbox.sendKeys("T-shirts");

driver.findElement(By.name("submit_search")).click();

//linkText & partial linkText
//driver.findElement(By.linkText("Printed Chiffon Dress")).click();
driver.findElement(By.partialLinkText("Chiffon Dress")).click();

}

}

Example 2:
---------------------------------------------
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class LocatorsDemo2 {

public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver","C:\\Drivers\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("http://automationpractice.com/index.php");

driver.manage().window().maximize(); // maximize web page

//className
int sliders=driver.findElements(By.className("homeslider-container")).size();
System.out.println(sliders);

//TagName
int links=driver.findElements(By.tagName("a")).size();
System.out.println(links);
}

}

Example3
-------------------------------------
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class LocatosDemo3 {

public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver","C:\\Drivers\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();

driver.get("https://www.facebook.com/");
driver.manage().window().maximize(); // maximize web page

//Tag & ID
//driver.findElement(By.cssSelector("input#email")).sendKeys("David");
//driver.findElement(By.cssSelector("#email")).sendKeys("David");

// Tag & Class
//driver.findElement(By.cssSelector("input.inputtext")).sendKeys("John");
//driver.findElement(By.cssSelector(".inputtext")).sendKeys("John");

//Tag & attribute
//driver.findElement(By.cssSelector("[name=email]")).sendKeys("Smith");
//driver.findElement(By.cssSelector("input[name=email]")).sendKeys("Smith");

//Tag , class & attribute
driver.findElement(By.cssSelector("input.inputtext[data-testid=royal_email]")).sendKeys("Smith");
driver.findElement(By.cssSelector("input.inputtext[data-testid=royal_pass]")).sendKeys("abc");

}

}

Followers