Google News
logo
Selenium - Interview Questions
Explain some types of Web locators in Selenium.
Locator is a command that tells Selenium IDE which GUI elements ( say Text Box, Buttons, Check Boxes, etc) it needs to operate on. Locators specify the area of action.
 
Locator by ID : It takes a string parameter which is a value of the ID attribute which returns the object to findElement() method.
 
  driver.findElement(By.id(“user”));
 
Locator by the link : If your targeted element is a link text then you can use the by.linkText locator to locate that element.
 
  driver.findElement(By.linkText(“Today’s deals”)).click();
 
Locator by Partial link : The target link can be located using a portion of text in a link text element.
 
  driver.findElement(By.linkText(“Service”)).click();
 
Locator by Name : The first element with the name attribute value matching the location will be returned.
 
  driver.findElement(By.name(“books”).click());
 
Locator by TagName : Locates all the elements with the matching tag name
 
  driver.findElement(By.tagName(“button”).click());
 
Locator by classname : This finds elements based on the value of the CLASS attribute. If an element has many classes then this will match against each of them. 
 
  driver.findElement(By.className(“inputtext”));
 
Locator by XPath : It takes a parameter of String which is a XPATHEXPRESSION and it returns an object to findElement() method.
 
  driver.findElement(By.xpath(“//span[contains(text(),’an account’)]”)).getText();
 
Locator by CSS Selector : Locates elements based on the driver’s underlying CSS selector engine.
 
  driver.findElement(By.cssSelector(“input#email”)).sendKeys(“myemail@email.com”);
Advertisement