Home >>Selenium Tutorial >Selenium WebDriver - Locating Strategies

Selenium WebDriver - Locating Strategies

Selenium WebDriver - Locating Strategies

Just like Selenium IDE, WebDriver uses the same collection of locating techniques for determining position of a specific web element.

Because, we are using WebDriver with java; each locating technique has its own command in Java to locate the web element.

Using findElement() and findElements() methods provided by WebDriver and WebElement class, locating elements in Selenium WebDriver is executed.

  • Based on the search criteria specified, findElement() returns a WebElement object or ends up throwing an exception if no element matches the search criteria.
  • findElements() is used to returns a matching list of WebElements to search the criteria. If no elements are found, it returns an empty list.
  • The following table lists all the Java syntax for locating elements in Selenium WebDriver.
Method Syntax Description
By ID driver.findElement(By.id (<element ID>)) It is used to Locates an element using the ID attribute
By name driver.findElement(By.name (<element name>)) It is used to Locates an element using the Name attribute
By class name driver.findElement(By.className (<element class>)) It is used to Locates an element using the Class attribute
By tag name driver.findElement(By.tagName (<htmltagname>)) It is used to Locates an element using the HTML tag
By link text driver.findElement(By.linkText (<linktext>)) It is used to Locates a link using link text
By partial link text driver.findElement(By.partialLinkText (<linktext>)) It is used to Locates a link using the link's partial text
By CSS driver.findElement(By.cssSelector (<css selector>)) It is used to Locates an element using the CSS selector
By XPath driver.findElement(By.xpath (<xpath>)) It is used to Locates an element using XPath query

No Sidebar ads