Skip to content

Instantly share code, notes, and snippets.

@erajanraja24
Created October 1, 2017 12:36
Show Gist options
  • Save erajanraja24/10bc58967b0527bc72cabfeca60274b3 to your computer and use it in GitHub Desktop.
Save erajanraja24/10bc58967b0527bc72cabfeca60274b3 to your computer and use it in GitHub Desktop.
Locators in Selenium Webdriver
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Ex1 {
public static void main(String[] args) throws InterruptedException {
WebElement textBox;
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://www.google.co.in");
driver.manage().window().maximize();
//Locate By Id
/*textBox=driver.findElement(By.id("lst-ib"));
textBox.sendKeys("Amarindaz");*/
//Locate By Name
/*textBox=driver.findElement(By.name("q"));
textBox.sendKeys("Amarindaz");*/
//Locate By Class Name
/*textBox=driver.findElement(By.className("gsfi"));
textBox.sendKeys("Amarindaz");*/
//Locate by Tag Name
/*List<WebElement> allLinks=driver.findElements(By.tagName("a"));
for(int i=0;i<allLinks.size();i++)
{
System.out.println(allLinks.get(i).getText());
}*/
//Link Text
//driver.findElement(By.linkText("Gmail")).click();;
//Partial Link Text
//driver.findElement(By.partialLinkText("Google"));
//By CSS
/*textBox=driver.findElement(By.cssSelector("input#lst-ib"));
textBox.sendKeys("Amarindaz");*/
//By xpath
textBox=driver.findElement(By.xpath("//input[@id='lst-ib']"));
textBox.sendKeys("Amarindaz");
Thread.sleep(2000);
driver.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment