Locators

1. ID Locator->

From below code, we have to find the  ID locator.



<input id="reg-firstname" class="reg-firstname" name="firstName" aria-required="true" tabindex="1" type="text">


In below code, we run the script  by using of ID locator.  


package mypackage;

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

public class Test {

 public static void main(String args[]) {

  String exepath = "C:\\Users\\abc\\Downloads\\chromedriver_win32//chromedriver.exe";

  System.setProperty("webdriver.chrome.driver", exepath);

  WebDriver driver = new ChromeDriver();

  // launch chrome and direct it to the URL
  driver.get("http://www.testingtreasure.com");

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

  // Finding element by ID locator
  driver.findElement(By.id("reg-firstname")).sendKeys("firstname");


  // close browser
  driver.close();

 }

}



2. Name Locator->


In below code, we run the script  by using of name locator.  


package mypackage;

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

public class Test {

 public static void main(String args[]) {

  String exepath = "C:\\Users\\abc\\Downloads\\chromedriver_win32//chromedriver.exe";

  System.setProperty("webdriver.chrome.driver", exepath);

  WebDriver driver = new ChromeDriver();

  // launch chrome and direct it to the URL
  driver.get("http://www.testingtreasure.com");

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

  // Finding element by name locator
  driver.findElement(By.name("firstName")).sendKeys("firstname");


  // close browser
  driver.close();

 }

}

3. Link Text-> 

<a href="http://www.testingtreasure.com">software testing?</a>

driver.findElement(By.linkText("software testing?"));


4. Partial link text-> 


<a href="http://www.testingtreasure.com">software testing?</a>

driver.findElement(By.PartialLinkText("software testing?"));


5. CSS class name->

driver.findElement(By.className("reg-firstname")).sendKeys("by class name");

6. CSS class selector-> 


driver.findElements(By.cssSelector("input[id=email']"));


7. Xpath->



driver.findElement(By.xpath(".//*[@id='reg-firstname']"));

No comments:

Post a Comment