☰ See All Chapters |
Locators in Selenium | Types of Locators
For applying selenium command we need to provide the target either it is web page or an element (Text field, Check Boxes, Select drop down, Buttons etc.) in the web page. To locate the target we have to identify the property which is unique to the target. This property should be unique from all the html code which has been rendered. Using this unique property we can create the command to locate the element. While identifying the unique property we can consider any attribute of the html tag, or we can frame combination of attributes of the html tag, but final it should be unique from the all html code in the current page.
Most of the time developers attach the id attribute and which will be unique to the page. If id attribute is not present we can consider name or class.
When name/id/class attribute is not available we have to use unique attribute attached only with that tag or combination of attributes and values to get the unique identification. In this case we have to use xpath syntax.
To identify the unique identification property we can use browser developer tool to inspect the element. Right click on the element and select “Inspect Element from the browser”.
We have 8 types of locators in Selenium using which we can locate the target
id
name
tagName
className
linkText
partialLinkText
xpath
css
And below is the priority to use these locators:
id
name
linkText / partialLinkText
css
xpath
We recommend not using className and tagName locators. Because className and tagName might have been used only once in a page at the time of developing automation script, but it is not safe, developer may use same tags and class at any time. You will learn about all the locators in next chapters.
Before we start learning to extract the target, we should know below some important key basics about the HTML:
In HTML language we have only tags. A tag opened must be closed. For example <head>………………….</head>
Syntax: <tagname attribute="value" attribute="value" attribute="value"> </tagname>
Tags can be nested (tag inside tag), however they must be opened and closed in order.
Tags may/may not have attributes.
Attributes may/may not have values.
Values are written inside double quotes/single quotes.
tagname, attribute names are defined by html language can be random.
value defined by developers and can be anything.
We consider below sample HTML code to provide you the example:
<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Signup</title> </head> <body> <div class="signup-container-styles"> <form name="signup" id="signup" action="/submit">
First Name : <input type="text" name="firstname" ></br>
Last Name : <input type="text" name="lastname"></br>
Date of Birth: <input type="text" class = "datePicker"></br>
Email: <input type="text" placeholder="This will be your login ID"></br>
Address : <textarea rows="4" cols="33" ></textarea></br>
Phone: <input type="text" id="phone" ></br>
<input type="submit" value="Submit">
</form> </body> </html> |
All Chapters