Locating WebElements

Element-locating functions are the building blocks of Selenium tests. These methods handle Ajax calls using timeouts and wait conditions to search and locate elements within a web page. There is no fixed strategy that you can follow to locate an element; it depends on the user ideology and their comfort level in locating the web elements.

An element can be located using any kind of locator type. Selenium WebDriver uses locators to interact with elements present in a web page. The following is the list of locator types used in Selenium WebDriver to locate web elements:

  • By.id
  • By.name
  • By.xpath
  • By.cssSelector
  • By.className
  • By.linkText
  • By.tagName
  • By.partialLinkText

The following is the syntax to use locatorType:

driver.findElement(By.locatorType("path") ...

Get Selenium Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.