Different Types of Locators in Selenium.
Hello All,
In this blog, am going to explain different types of Locators in Selenium and how it is used to inspect the WebElement.
Let’s start with a short introduction about Locators.
Locators:
Selenium is the automated testing tool which is used to validate the web applications using different browsers and platforms. Locators are one of the essential components of Selenium infrastructure, which help Selenium scripts in uniquely identifying the WebElements(such as text box, button, checkbox etc.,) which are then used to perform the action.
Locating elements in Selenium WebDriver is performed with the help of findElement() and findElements() methods provided by Selenium WebDriver and WebElement class.
- findElement() method is used to find the single WebElement from the Webpage and perform actions on it.
driver.findElement(By.LocatorStrategy(“Locator Value”)
2. findElements() method is used to retrieve the list of WebElements from the Webpage. This method returns the list of WebElements through the list interface, we can iterate it and perform operations on it.
List <WebElement> elementname = driver.findElements(By.LocatorStrategy(“Locator Value”))
Let’s see the different types of locators in Selenium:
Let’s see how to use these locators with examples:
ID:
ID locator is used to locate the WebElement by the value of the id attribute. Mostly the ids are unique for the WebElement. Here we need to pass the value of the id attribute as the parameter.
syntax: driver.findElement(By.id(“element id”))
example: driver.findElement(By.id(“email”))
Name:
Name locator is used to identify the WebElement by the value of the name attribute. If there are multiple elements with the same Name locator then the first element on the page is selected.
syntax: driver.findElement(By.name(“element name”))
example: driver.findElement(By.name(“email”))
ClassName:
Here the WebElements are accessed with the help of the class name attribute.
syntax: driver.findElement(By.className (“element class”))
example: List <WebElement> classname = driver.findElements(By.className(“inputtext _55r1 _6luy”))
TagName:
TagName locator is used along with the findElements() method to identify multiple similar items in the Webpage. It is used to locate the WebElements with the help of the HTML tags.
syntax: driver.findElement (By.tagName (“HTML tag name”));
example: List<WebElement> lists=driver.findElements(By.tagName(“a”))
Link Text:
Link Text is used to identify the element with the exact text.
syntax: driver.findElement(By.linkText (“linktext”))
example: driver.findElement(By.linkText(“Pharmacy”))
PartialLinkText:
This method helps to locate the element that contains the part of the link text.
syntax: driver.findElement(By.partialLinkText (“partiallinktext”))
Example: driver.findElement(By. partialLinkText(“Best”))
driver.findElement(By. partialLinkText (“Service”))
Xpath:
XPath stands for XML Path Language. XPath is used to navigate through the HTML structure of the page. XPath can be used for both HTML and XML documents to find the location of any element on a webpage using HTML DOM structure.
syntax: driver.findElement(By.xpath (<xpath>))
Xpath expression syntax:
//tagname[@attribute=’value’]
Example: driver.findElement(By.xpath(//input[@id=’email’])
CSS Selector: CSS stands for Cascading Style Sheet
A CSS Selector is a combination of an element selector and a value which identifies the web element within a web page. They are string representations of HTML tags, attributes, Id and Class.
syntax: driver.findElement(By.cssSelector(“css selector”))
Example: driver.findElement(By.cssSelector(“input#email”))
CSS Selector can also be accessed using the following strategies: ID, Class, Attribute, Substring, InnerText. We can discuss about these strategies in my next blog.
Reference:
Hope this blog helps you to find the WebElements from the Webpage using locators. Thanks for reading! Happy Testing!