Tuesday, September 27, 2016

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.

MethodSyntaxDescriptionBy IDdriver.findElement(By.id(<element ID>))Locates an element the using ID attributeBy namedriver.findElement(By.name(<element name>))Locates an element using the Name attributeBy class namedriver.findElement(By.className(<element class>))Locates an element using the Class attributeBy tag namedriver.findElement(By.tagName(<htmltagname>))Locates an element using the HTML tagBy link textdriver.findElement(By.linkText(<linktext>))Locates link using link textBy partial link textdriver.findElement(By.partialLinkText(<linktext>))Locates link using link's partial textBy CSSdriver.findElement(By.cssSelector(<css selector>))Locates element using the CSS selectorBy XPathdriver.findElement(By.xpath(<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).

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).

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.

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.

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.

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.

driver.findElement(By.xpath(".//*[@id='content']/table[1]/tbody/tr/td/table/tbody/tr[2]/td[1]/input")).sendkeys("100");

No comments:

Post a Comment