Locators In Selenium
When we automate the web-based applications, the automation tool should capable of performing the operations on the HTML elements. So, now the question comes, how do these automation tools identify on which HTML element they need to perform a particular operation? The answer to this is "Locators in Selenium".
Locators :
As discussed above, To perform the actions on web element,we need to identify the web element present on the web page. So, to identify WebElements on web page we have to use locators. Locators help Selenium scripts in uniquely identifying the WebElements(such as text box, button, etc.) present on the web page.
Different Types of Locators in Selenium WebDriver :
Selenium WebDriver provides different web locators for locating WebElements on the page. Here are the different locators in Selenium WebDriver.
The ID is one of the fastest and unique ways of locating web elements and is considered as one of the most reliable methods for determining an element.
Syntax
driver.findElement(By.id(“Id_Value”));
Similar to the id attribute, Selenium also offers users a way to identify the element using the name attribute.
Syntax
driver.findElement(By.name(“name_Value”));
ClassName allows Selenium to find web elements based on the class values of the DOM.
Syntax
driver.findElement(By.className(“class_Value”));
This locator type uses tag names to identify elements in a web page. TagName means HTML tags like button, div, input etc.
Syntax
driver.findElement(By.tagName(“html_Tag”));
Now most of the web pages are dynamically designed. Thus its quite challenging to get a unique id, name, or class to locate element. In this type of situation, CSS selectors can be a great alternatives.
Syntax
driver.findElement(By.cssSelector(“css_Value”));
LinkText locate web elements by using the hyperlink texts. We can only use this for elements containing anchor (<a>) tags.
Syntax
driver.findElement(By.linkText(“text_of_Link”));
This is quite similar to the LinkText. But, Partial link text can also recognize the element by using part of the hyperlink text.
Syntax
driver.findElement(By.partialLinkText(“Partial_Text_of_Link”));
XPath uses the XML expression to locate an element on the webpage. Xpath can access any element present in the webpage even when they have dynamic properties.
Syntax
driver.findElement(By.xpath(“xpath_Value”));
There are different ways to write XPath :
XPath by Attributes : In this we use attribute `Name` and `value` to get XPath.
Syntax
driver.findElement(By.xpath(“//tagname[@attribute_name=`attribute_value`]”));
XPath by Text : In this we use html text to get XPath.
Syntax
driver.findElement(By.xpath(“//tagname[text()=`html_text`]”));
XPath by Index : This is used, When there are multiple web element present for same xpath.
Syntax
driver.findElement(By.xpath(“(//tagname[@attribute_name=`attribute_value`])[index]”)); </br> driver.findElement(By.xpath(“(//tagname[text()=`html text`]”))[index]);
XPath by contains : When we want to use some part of attribute or text to get xpath then we use `contains` function.
Syntax
driver.findElement(By.xpath(“//tagname[contains(@attribute_name,`part_of_attribute_value`)]”)); </br> driver.findElement(By.xpath(“//tagname[contains(text(),`part_of_html_text`)]”));