Wednesday, October 29, 2014

Chapter 21- Selenium - Locators

Selenium - Locators

Locating elements in Selenium WebDriver is performed with the help of findElement() and findElements() methods provided by WebDriver and WebElement class.
  • The findElement() method returns a WebElement object based on a specified search criteria or ends up throwing an exception if it does not find any element matching the search criteria.
  • The findElements() method returns a list of WebElements matching the search criteria. If no elements are found, it returns an empty list.
The Below table gives the java syntax for locating elements in selenium Webdriver.
By IDdriver.findElement( an element the using ID attribute
By namedriver.findElement( an element using the Name attribute
By class namedriver.findElement(By.className())Locates an element using the Class attribute
By tag namedriver.findElement(By.tagName())Locates an element using the HTML tag
By link textdriver.findElement(By.linkText())Locates link using link text
By partial link textdriver.findElement(By.partialLinkText())Locates link using link's partial text
By CSSdriver.findElement(By.cssSelector())Locates element using the CSS selector
By XPathdriver.findElement(By.xpath())Locates element using XPath query

Locators Usage

Now let us understand the practical usage of each one of those locators methods with the help of
1. By Id : The Object is accessed with the help of ID. In this case, it is the ID of the text box. The Value is entered into the text using sendkeys method with the help of ID(cdensity).
Selenium IDE 84
2. By name : The Object is accessed with the help of name. In this case, it is the name of the text box. The Value is entered into the text using sendkeys method with the help of ID(cdensity).
Selenium IDE 85
3. By Class name : The Object is accessed with the help of Class Name. In this case, it is the Class name of the WebElement. The Value can be accessed with the help of gettext method.
Selenium IDE 86
List<WebElement> byclass = driver.findElements(By.className("smalltext smtb"));
4. By Tag name : The DOM Tag Name of the element and it is very easy to handle table with the help of this method. We can look at an example out of the demo app.
WebElement table = driver.findElement("calctable"));
List<WebElement> row = table.findElements(By.tagName("tr"));
int rowcount = row.size();
5. By Link Text : This methods helps us to find the link element with matching visible text.
Selenium IDE 87
5. By partial link text: This methods helps us to Find the link element with partial matching visible text.
Selenium IDE 87
6. By CSS : The CSS is used as a method to identify the webobject, however NOT all browsers support CSS identification.
WebElement loginButton = driver.findElement(By.cssSelector("input.login"));
7. By Xpath : XML stands for XML path language, is a query language for selecting nodes from an XML document. The XPath language is based on a tree representation of an XML document and provides the ability to navigate around the tree by selecting nodes using a variety of criteria.
Selenium IDE 88

No comments: