Tuesday, June 14, 2016

Understanding Locators in Selenium WebDriver and common element types

Locators are used to identify an element in the WebPage.  An object can be identified based on the attributes values.  In this article, we will discuss the different locators based on which an element can be identified in the page using selenium webdriver.


Locators are used to locate an element in the Page based on the unique address of the object in the Page. It can be unique attribute value, for e.g: id of the element or the absolute or relative xpath of the element in the Page. 


Once an object is identified in the Page, we can perform the required operation/action on the object. Below are the different common types of elements on the Page:


Common Elements Types
Type
Common Operations on Object Type
Anchor elements or links
Clicking on a link
Validating links in Page are correct or broken
Validating links exist in the Page
TextBox
Validating text box is available/enabled
Input data in the edit box
Radio Button
Selecting a radio button
Checkbox
Validating whether checkbox is selected or not.
Selecting/deselecting checkbox
DropDown
Selecting value from the dropdown
Labels
Extracting value from the label or validating label exist in the page.
Table
Extracting information from table.
Multi-Select ComboBox
Selecting multiple values from a combobox.


The different types of locators used to locate elements in selenium are described below:


Locator Types
Locator DefinitionUsage
IDelement matching "id" attribute of the element. Normally id of an element in the Page is expected to be unique. Hence if available, using id of the element is good option.
WebElement usr = driver.findElement(By.id("name"));
 Nameelement matching "name" attribute of the element. There may be multiple element with same name in Page. In such scenario, first element matching the name is selected.
WebElement usr = driver.findElement(By.name("uname"));
 Link Text
Partial Link text
For links in the Page, we can use linktext and partialLinkText to identify the links based on the name/Partial text of the link. Applicable for links element only

WebElement elem=driver.findElement(By.linkText("mail"));

driver.findElement(By.partialLinkText("mail"));
 CSS SelectorUsing CSS locators, we can identify elements. Both Xpath and CSS are useful and highly used in identifying elements in the Page.Absolute or relative Xpath can be created to identify element in the PageWebElement elem = driver.findElement(By.cssSelector("a.test"));
 XPathPlease refer to my previous post for details on xpath and css:
http://seleniumbites.blogspot.in/2016/01/xpath-and-css-for-selenium-webdriver.html
WebElement elem = driver.findElement(By.xpath("//input[@id='mail']"));
ClassNameClassName uses the class attribute of the element to locate the element. Foe e.g.: in case of identifying the random error message displayed in the Page, usually the message may have the same class attribute. We can create a collection of elements matching the class and gather the required error messages in the Pagedriver.findElement(By.className("errormessage"));
TagNameThis locator is useful while working with list of elements in the Page. E.g: Finding number of links in the PageList <WebElement> linklst = driver.findElements(By.tagName(“a”));