Locators in Selenium

To locate a web element we use  findElement method e.g. driver.findElement(By.id(“idname”)). Here the id is a locator there total 8 locators supported by selenium

Locator HTML Tag Accessing using selenium
id <input type=”text” id=”email” /> WebElement Element = driver.findElement(By.id(“email”));
name <input type=”text” name=”email” /> WebElement Element = driver.findElement(By.name(“email”));
LinkText < a href=http://google.com> My link </a> WebElement Element = driver.findElement(By.linkText(“My Link”));
Partial

LinkText

< a href=http://google.com> My link </a> WebElement Element = driver.findElement(By.partialLinkText (“My “));
TagName <input type=”text” name=”email” /> WebElement Element = driver.findElement(By.tagName (“input”));
ClassName <input type=”text” name=”email”  class=”css”/> WebElement Element = driver.findElement(By.className (“css”));
css elector css=input[id=email] WebElement Element = driver.findElement(By.cssSelector(“input[id=email’]”));
xpath Click here for xpath tutorial WebElement Element = driver.findElement(By.xpath(“//tr/td “));