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.
MethodSyntaxDescription
By IDdriver.findElement(By.id())Locates an element the using ID attribute
By namedriver.findElement(By.name())Locates 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 http://www.calculator.net
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
driver.findElement(By.id("cdensity")).sendKeys("10");
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
driver.findElement(By.name("cdensity")).sendKeys("10");
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(By.id("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
driver.findElements(By.linkText("Volume")).click();
5. By partial link text: This methods helps us to Find the link element with partial matching visible text.
Selenium IDE 87
driver.findElements(By.partialLinkText("Volume")).click();
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
driver.findElement(By.xpath(".//*[@id='content']/table[1]/tbody/tr/td/table/tbody/tr[2]/td[1]/input")).sendkeys("100");

No comments: