Locator Objects
When we want to Automate the web applications, we need to identify the HTML elements and perform some actions on it. To do it happen, Locators play a role.
Locators are commands used to instruct Selenium which GUI it should perform an action on.
There are various types of locators available to locate elements. We can use any of the below mentioned locators to find element and perform action on it.
Locators Type:
- ID
- TagName
- XPath
- CSS
- Name
- linkText
- PartialLinkText
- ClassName
Find Element and Find Elements:
Find Elements used to find all the unique elements in the webpage. Find Element is used to identify the specific element in the webpage. When the element was not identified during search, the findElement method throws a "NoSuchElementException" exception Whereas, the findElements method returns an empty list.
Let’s learn about “driver” Object and Find Elements in my upcoming blog post. Now, we can focus on how to identify Element in the HTML page.
- By ClassName: driver.findElement(By.className(“ _2zrpKA _1dBPDZ”));
It matches the values mentioned in the class attributes.
2. By ID : driver.findElement(By.id(“twotabsearchtextbox”));
3.By Name : driver.findElement(By.name(“url”));
4. By LinkText : driver.findElement(By.linkText(“see more”));
By Partial LinkText: driver.findElement(By.partialLinkText(“more”));
The difference between link Text and Partial LinkText is that Link text is used to identify hyperlinks of the webpages. It should match with the exact string defined under href attribute. Where as partial link text, search with the partial match of the string.
Note : linkText and partialLinkText are case sensitive.
5. By TagName: driver.findElement(By.tagName(“input”));
6. By Xpath :
They are of two types
Absolute :
It consists of a complete path from the root of the DOM structure: For example, to access Gmail in Google homepage, Here is the complete path
for example : /html/body/div[1]/header/div/div[1]/div[2]/div/form/div[2]/div[1]/input
Relative(Custom XPath):
There are many ways to find Relative XPath. I will be sharing few which are most used while finding locators.
It references the specific element we are looking for. Mostly, we prefer to go with relatives as there are challenges with Absolute. Absolute path will contains complete root node in the DOM structure and if any changes in the root level of the DOM can cause the Absolute path invalid. So, we may end up with "Element not found" error. It is recommended to use Relative XPath.
Syntax to find Xpath of other types:
Xpath= //Tag_name[@attribute_name=’Attribute_value’]
1. Contains: Xpath=//*[contains(@name,'btn')]
2. Using OR conditions: Xpath=//*[@type='submit' or @name='btnReset']
3. Using AND conditions: Xpath=//input[@type='submit' and @name='btnLogin']
4. Starts with : Xpath=//label[starts-with(@id,'message')]
By CSS Locator:
They are of many types. I will be sharing a few most used methods of finding elements using CSS Locators.
1.Tag and ID : css=tag#id . Hash tag should be used between tag and ID
if Tag is Input and ID is “pswrd”, then CSS = Input#pswrd
2. Tag and class : css=tag.class
3 .Tag and attribute : css=tag.class
4. Tag, class, and attribute : css=tag.class[attribute=value]
5. Inner text :css=tag:contains("inner text")
In my next blog we can look into ( Find Elements, Selenium Methods).
Thank you!!!
Please do share your comments for improvements and learnings :)