What is Selenium?
Selenium is one of the most widely used open-source automation tool. In addition, it supports automation across different browsers, platforms, and programming languages. It can be easily install on platforms such as Windows, Linux, Solaris, and also Macintosh. Likewise, it also supports OS (Operating System) for mobile applications like iOS, windows mobile, and android.
What are Locators?
A locator is a way to identify elements on a page. Together with It is the argument pass to the finding elements methods. As well as Locating elements in Selenium WebDriver is perform with the help of findElement() and findElements().
Methods provided by WebDriver and WebElement class:
findElement() returns a WebElement object based on a specified search criterion or ends up throwing an exception Likewise if it does not find any element matching the search criteria.
findElements() returns a list of WebElements matching the search criteria. Likewise, If no elements are found, it returns an empty list.
Locators in selenium are:
2. CSS Selector
6. PartialLink text
This locator in selenium is the most preferred and fastest way to locate desired WebElements on the page. ID Selenium locators are unique for each element in the DOM.
Since IDs are unique for each element on the page, then it is consider the fastest and safest method to locate elements.
Syntax: driver.findElement(By.id (<element ID>));
Steps for finding id locators in a website are :
a. First we visit the website and go to the particular field for finding an id locator and click on the right button of the mouse and select the inspect option from the list.
b. And then we visit the below page and choose the id selector from there.
WebElement first= driver.findElement(By.id(“username”));
CSS (Cascading Style Sheets) is use to style web pages. At the same time, CSS is also one of the widely-used ways to locate WebElements in the DOM.
CSS Selector in Selenium should have opted if you cannot locate an element using ID or Name locators. It can be chosen over the XPath locator.
syntax : driver.findElement(By.cssSelector (<css selector>));
Class Name locator is use for locating WebElements that are define using the class attribute.
syntax : driver.findElement(By.className (<element class>));
An element can be defined via multiple attributes, one such attribute is Name. Name locator in Selenium WebDriver can also be use to locate elements like an ID locator.
Unlike ID locators, which are unique for a page, the Name locator may or may not have a unique value. If there are WebElements with the same name, the locator selects the first element with that name on the page.
Synatx: driver.findElement(By.name (<element name>));
Elements can be located via link text that is present in the hyperlinks. For example, the first link will be selected in a scenario where there are multiple links of the same text.
However, this Identifier strategy can only be use for elements that have an anchor(a) tag.
synatx: driver.findElement(By.linkText (<linktext>));
6. PartialLink text:
There is a provision to locate a web element using Partial Link Text akin to the normal Link Text locator in Selenium. Locating WebElements using partial link text is prefer when the link text is too long.
Here, the partial text helps identify a unique element and use it to perform further actions on it. Sometimes, using this can also be to locate multiple links on a page with a common partial text.
syntax: driver.findElement(By.partialLinkText (<linktext>))
The tag Name locator is used to find the elements matching the specify Tag Name. It is very helpful when we want to extract the content within a Tag. syntax:
findElement(By.tagName(“HTML Tag Name”));
It helps in locating elements on the web page using XML expressions. The basic syntax used for using XPath as a CSS locator in Selenium WebDriver is shown below:
XPath: //tagname[@attribute = ‘value’]
syntax: driver.findElement(By.xpath (<XPath>));
In the all above locators best locators are ID because IDs are the most reliable locator option in web design in that they are guaranteed to be unique on the page by W3C standards. You can’t have two elements with the same ID within one page.