Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Types of Element Locators in Selenium Webdriver

Today, we are going to discuss how to use locator in selenium WebDriver. Basically locators are the basic building blocks of selenium WebDriver, as it is used for identification of web element/object from the web pages and also these locators provides simple and effective way to interact with web pages using simple commands.

One more thing, there are different type locators in selenium WebDriver, while automating the test cases or test scenarios's, we need to select right locator to uniquely identify the web element/objects because it eventually helps us to run the test scenarios's in more faster and effective way and also it is more reliable. Sometime it reduces the maintenance cost and effort.

Different types of Locators in Selenium are as follows:
  • ID
  • Name
  • Class Name
  • Tag Name
  • Link Text & Partial Link Text
  • CSS Selector
  • XPath
To inspect an element you just have to open the desired web page, right-click the desired element and click on Inspect Element. A new panel will open showing the desired element. Also you can inspect other elements by clicking on the cursor in the top left side of the Developer Tools or Firebug panels and hovering page elements.

Locating elements in WebDriver is done by using the method "findElement(By.locator())".

Lets see the below example for the selenium locator :

ID Locator:
ID’s are unique for each element so it is common way to locate elements using ID Locator. As per W3C, ID’s are supposed to be unique on a page and it makes ID’s are the most reliable locator. ID locators are the fastest and safest locators out of all locators.
Example :
id = id of the element

Web Element present in browser :
 id="user" class="user" type="text">

Code snippet to use ID Locator in WebDriver:
WebElement element = driver.findElement(By.id("user"));

Name Locator:
We sometimes use Name locator to identify the elements on our webpage. Locating elements using Name is same as locating elements using ID locator.
These are not unique on a page. If there are multiple elements with the same Name locator then the first element on the page is selected. Test may fail, if another element with the same Name locator is present on the web page or added by the developers in the later stages.
Example :
name = Name of the element

Web Element present in browser :
 name="user" class="user" type="text">

Code snippet to use Name Locator in WebDriver:
WebElement element = driver.findElement(By.name("user"));

Class Name Locator:
Class Name locator gives the element which matches the values specified in the attribute name "class".
Example :
class = class of the element

Web Element present in browser :
 name="username" class="user" type="text">

Code snippet to use Class Locator in WebDriver:
WebElement element = driver.findElement(By.className("user"));

Tag Name Locator:
Tag Name locator is used to find the elements matching the specified Tag Name. It is very helpful when we want to extract the content within a Tag.

Web Element present in browser :
 href="one.php">one 
href="two.php">two

Code snippet to use Tag Name Locator in WebDriver:
List WebElement> list = driver.findElements(By.tagName("a"));
System.out.println("Number of links: "+list.size());

Link Text Locator:
It is a perfect way to find the links on a page. This Link Text locator works only on links (hyperlinks) so it is called as Link Text locator.

Web Element present in browser :
 href="Ajax.php">Learn ajax with example

Code snippet to use Link Text Locator in WebDriver:
WebElement item = driver.findElement(By.linkText("Learn ajax with example"));

Partial Link Text:
In some situations, we may need to find links by particular portion of the text in a Link Text element. In such situations, we use Partial Link Text to locate elements.

Web Element present in browser :
 href="Ajax.php">Learn ajax with example

Code snippet to use Partial Text Locator in WebDriver:
WebElement item = driver.findElement(By.linkText("Learn ajax"));

CSS Selector Locator:
CSS selector makes the execution of script faster compared to XPath locator. This locator is always the best way to locate elements on the page.

Web Element present in browser :
 id="user" class="user" type="text">

Code snippet to use CSS Selector Locator in WebDriver:
WebElement item = driver.findElement(By.cssSelector("input#user"));

Following are the mostly used formats of CSS Selectors.

Tag and ID
driver.findElement(By.cssSelector(tag#id))

Tag and Class
driver.findElement(By.cssSelector(tag.class))

Tag and Attribute
driver.findElement(By.cssSelector(tag[attribute=value]))

Tag, Class and Attribute
driver.findElement(By.cssSelector(tag.class[attribute=value]))

XPath Locator :
While DOM is the recognized standard for navigation through an HTML element tree, XPath is the standard navigation tool for XML; and an HTML document is also an XML document (xHTML). XPath is used everywhere where there is XML.

Web Element present in browser :
 id="user" class="user" type="text">

Code snippet to use XPath Locator in WebDriver:
WebElement item = driver.findElement(By.xpath("//*[@id='user']"))

Following are the mostly used formats of XPath Selectors.
1. WebElement item = driver.findElement(By.xpath("//*[@id='username']"))
2. WebElement item = driver.findElement(By.xpath("//input[@id='username']"))
3. WebElement item = driver.findElement(By.xpath("//form[@name='loginForm']/input[1]"))
4. WebElement item = driver.findElement(By.xpath("//*[@name='loginForm']/input[1]"))

Hope you like this simple example for Locators in Selenium Webdriver. Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section.





This post first appeared on Learn Programming, please read the originial post: here

Share the post

Types of Element Locators in Selenium Webdriver

×

Subscribe to Learn Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×