Locators in Selenium WebDriver

Learn more about different locators in Selenium

Commonly Used Locators in Selenium WebDriver

Locators are defined as an address that identifies a web element uniquely within the webpage.

Different types of locators in Selenium WebDriver are listed below:

1) By id – It is the most preferred way to locate any element on the webpage as it is supposed to be unique all the time. It is also the fastest and most reliable way of locating a particular element.

WebElement element = driver.findElement(By.id(“username”));

2) By name – If there is no Id then name is considered to be the next best choice for locating any element but unlike Id, name is not always unique within a page. In case there are multiple elements with the same name, webdriver will perform action on the first matching element.

WebElement element = driver.findElement(By.name(“login”));

3)  By linkText – This type of locator only applies to hyperlinks in the webpage. If there are multiple links with same text then webdriver will perform action on the first matching element.

WebElement element = driver.findElement(By.linkText(“Register”));

4) By partialLinkText – This locator is similar to linkText but it only matches with a portion of the text for the links. If there are multiple matches, only the first match will be accessed.

WebElement element = driver.findElement(By.partialLinkText(“Register”));

5) By tagName – A tagName is a part of a DOM structure where every element on a page is been defined via tag like input tag, button tag, anchor tag, etc. Each tag has multiple attributes like ID, name, value class, etc. In the case of tagName locator in Selenium, we will simply use the tag name to identify an element.

List<WebElement> allLinks = driver.findElement(By.tagName(“a”));

Above command will return all the links in the webpage which has a tagname “a” and store in a list.

6) By className – Webdriver can locate elements on a webpage with their class name but multiple elements can have the same class name.

WebElement element = driver.findElement(By.className(“sample”));

7) By xpath – XPath uses path expressions to select nodes or node-sets in an XML document. The node is selected by following a path or steps.

WebElement element = driver.findElement(By.xpath(//div[@id=’login’]));

There are 2 types of xpath:

Absolute xpath: It is the direct way to find the element, but the disadvantage of the absolute XPath is that if there are any changes made in the path of the element then that XPath gets failed.

Relative xpath: It starts with the double forward slash (//), which means it can search the element anywhere at the webpage.

8) By cssSelector – theCSS Selector combines an element selector and a selector value that can identify particular elements on a web page.


Bijan Patel
Full Stack Test Automation Expert | Selenium Framework Developer | Certified Tosca Automation Specialist | Postman | DevOps | AWS | IC Agile Certified | Trainer | Youtuber | Blogger|

Launch your GraphyLaunch your Graphy
100K+ creators trust Graphy to teach online
QASCRIPT 2023 Privacy policy Terms of use Contact us Refund policy