Thursday 16 September 2021

What are locators and types of locators in Selenium?

 In Selenium Locators are the way to identify a HTML element on a web page.Locating elements in Selenium WebDriver is performed with  findElement() and findElements() methods.The main objective of using element locators is to identify the element  on the web page accurately.

findElement() returns a Web Element  based on a specified search criteria and throws an exception if it does not find any element matching the specified search criteria.

findElements() returns multiple Web Elements matching the search criteria. 

Selenium web driver uses eight locators to locate elements :

1.By id - Locates a web element using the ID attribute.

Syntax : driver.findElement(By.id (element ID))

2.By name -  Locates an element based on Name attribute.

Syntax : driver.findElement(By.name (element name))

3.By class name - Locates an element using class attribute.

Syntax : driver.findElement(By.className (element class))

4.By tag name - Locates an element using HTML tag.

Syntax : driver.findElement(By.tagName (htmltagname))

5.By CSS - Locates an element using CSS selector.

Syntax : driver.findElement(By.cssSelector (css selector))

6.By Xpath - Locates an element using Xpath.

Syntax : driver.findElement(By.xpath (xpath))

7.By link text - Locates a web element using link text.

Syntax : driver.findElement(By.linkText (linktext))

8.By Partial link text - Locates a web element using partial link text.

Syntax : driver.findElement(By.partialLinkText (linktext))