🦎Locators

Locators

Locators are used to identify web elements on a web page. They are used by WebDriver commands to find web elements.

circle-info

💡 Selenium command to operate on GUI elements; right-click the cursor anywhere to access the “Inspect” option

Locator Type
Description
Java Example

ID

Locates an element by its unique ID attribute

Class Name

Locates elements by matching the CSS class name

driver.findElement(By.className("className"));

Name

Locates elements by the name attribute

Tag Name

Locates elements by the HTML tag name

driver.findElement(By.tagName("tagName"));

Link Text

Locates anchor elements with exact link text

driver.findElement(By.linkText("linkText"));

Partial Link Text

Locates anchor elements with partial link text

driver.findElement(By.partialLinkText("partialLinkText"));

CSS Selector

Locates elements using CSS selectors

driver.findElement(By.cssSelector("cssSelector"));

XPath

Locates elements using XPath expressions

driver.findElement(By.xpath("xpathExpression"));

Last updated