Selenium Webdriver Locators

Selenium has earned the distinction as the most widely used test automation tool for testing web based applications across the globe. Selenium is highly user friendly and the execution can be done faster. Selenium has many added features to make testing easier and one among them is the locator. There are many locators in Selenium.

One should be familiar with the processes of Selenium testing to know the significance’s of locators in Selenium. The process is as follows.

  • Launch the browser
  • Open the website that is to be automated
  • Locate web elements such as text box
  • Perform actions on the located web elements such as writing in the text box
  • Perform assertion such as checking “Success” message.

Here you will find that locating elements is of great significance as the user can perform some action only when a desired element is located or identified. One need to have the knowledge about the HTML attributes of the elements to locate it. Either Firebug, a Firefox extension or the in-built developer tool can be used to get the HTML source code of the elements. There we will find the different attributes of the element such as name, id, link text etc. Once, getting the knowledge about the HTML attributes. Different types of locators in Selenium webdriver can be used for locating elements.

Locators in Selenium WebDriver

In total there are eight different types of locators in Selenium and they are as follows.

By ID: – Here the locators use the ID attribute to locate the elements.

WebElement element = driver.findElement(“elementId”));

By Class Name: – Here the locator uses the class name attribute

WebElement element = driver.findElement(By.className(“elementsClass”));

By Tag Name: – HTML tags such as div, a, input etc are used to locate the element

WebElement element = driver.findElement(By.tagName(“a”));

By Name: – Here the locator uses the name attribute to identify the element.

WebElement element = driver.findElement(“male”));

By Link Text: – Locates the web element of link type using their text

WebElement element = driver.findElement(By.linkText(“Click Here”));

By Partial Link Text: – Web element of link type will be located using partially matching text.

WebElement element = driver.findElement(By.partialLinkText(“Click”));

By CSSselector: – Here the CSS and its CSS patterns are used to locate the elements.

WebElement element = driver.findElement(By.cssSelector(“div#elementId”));

By XPath: – XPath is used to locate the elements.

WebElement element = driver.findElement(By.xpath(“//div[@id=’elementId’]”));

Here the most important thing is to select the right locator for your project. Selecting locator is depending on the uniqueness of the element you are searching for. ID locator is the most effective one as each element will have a unique ID but cannot be used always as one need to know the ID of the element. If you do not know the ID, they you can go with the other locators.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *