๐ŸฆŽLocators

Locators

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

๐Ÿ’ก Selenium command to operate on GUI elements; right-click the cursor anywhere to access the โ€œInspectโ€ option

Locator TypeDescriptionJava Example

ID

Locates an element by its unique ID attribute

driver.findElement(http://by.id/("elementId"));

Class Name

Locates elements by matching the CSS class name

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

Name

Locates elements by the name attribute

driver.findElement(http://by.name/("elementName"));

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