logo image

WebDriver Locators in selenium



First we have to know What is "By" ?

"By" class : By is static class in webdriver's API. It is used to locate elements with in the webpage. It has the following methods:

  • id(java.lang.String id) : Searches for an element having the specified value of the "id".
    driver.findElement(By.id("id attribute"))...
  • name(java.lang.String name) : Finds element based on the value of the "name" .
  driver.findElement(By.name("name attribute"))...
  • cssSelector(java.lang.String selector) : Find element (or) elements based on the value of the "cssSelector" attribute.
  List<WebElement> cssSelector = driver.findElements( By.cssSelector("input[class='***']");
  • tagName(java.lang.String name) : It Finds based on the "TagName".
  List<WebElement> TagName = driver.findElements( By.cssSelector("href");
  • className(java.lang.String className) : Finds based on the "className".
     driver.findElement(By.className("className attribute"))...
  • linkText(java.lang.String linkText) : Finds based on the "linkText".
  driver.findElement(By.linkText("linkText attribute"))...
  • partialLinkText(java.lang.String linkText) : Finds based on the "partialLintText".
  driver.findElement(By.partialLinkText("privacy"))...
  • xpath(java.lang.String xpathExpression) : Finds based on the "xpath".
  driver.findElement(By.xpath(".//*[@id='***']"))...