Reading:  

Getting started with Selenium


Getting started with Selenium Locators

For Locating elements in Selenium WebDriver,with the help of findElement() and findElements() methods provided by WebDriver and WebElement class.

  • findElement(): Returns a WebElement object based on a specified search criteria,if it does not find any element matching the search criteria ends up throwing an exception.
  • findElements(): returns a list of WebElements matching the search criteria. If no elements are found, it returns an empty list.

The Java Method syntax for locating elements in Selenium WebDriver:

By ID: Locates an element using the ID attribute

Ids are the preferred way to locate elements on a page for two main reasons:

  • Ids are supposed to be unique on an html page. This makes ids a very explicit and reliable way to locate elements on the page.
  • All browsers also have highly efficient methods to get an object on the page using their ids. This makes id locators the fastest type of locator in Selenium.

    Syntax: driver.findElement(By.id(<element ID>))

By name: The name attribute can be used to identify an element within a HTML document.

  • The difference between id and name locators though, name attributes don’t have to be unique in a page. If there are multiple elements with the same name, then just using the name would select the first element in the page. If you want to choose a specific element, you can filter it using a different attribute.

    Syntax: driver.findElement(By.name(<element name>))

 

By class name: Locates an element using the Class attribute.

        Syntax: driver.findElement(By.className(<element class>))

By tag name: To locate an element using the HTML tag.

        Syntax: driver.findElement(By.tagName(<htmltagname>))

By link text: This locator identifies elements by the text in them. This is usually used for hyperlinks.

        Syntax: driver.findElement(By.linkText(<linktext>))

By partial link text: Locates a link using the link's partial text

        Syntax: driver.findElement(By.partialLinkText(<linktext>))

By CSS: Locates an element using the CSS selector

         Mainly css locators can be used to identify a large number of variations.
         Syntax: driver.findElement(By.cssSelector(<css selector>))

By XPath: XPath is a very powerful language to express which element to target.

Locators Usage:

By ID:  Here we access an object with the help of IDs. In this case,

  • ID of test box is: “amount” as shown.
  • sendkeys method : to enter the values into the text box.

DOM Element id example

 

Example: 

driver.findElement(By.id("amount")).sendKeys("1000");

By Name: we access an object with the help of name. In this case,

  • The name of the text box is: "Amount"
  • Values are entered into the text box using the sendkeys method with the help of ID(amount).

Selenium web driver get element by name example

Example: 

driver.findElement(By.name("Amount")).sendKeys("1000");

 

By Class Name:

We access an object is accessed with the help of Class Name. In this case,

  • It is the Class name of the WebElement is: "H2wTR"
  • The Value can be accessed with the help of the gettext method.

Example: 

String result = driver.findElement(By.className("H2wTR")).getText();

 

By Tag Name:

The DOM Tag Name of an element can be used to locate that particular element in the WebDriver.

Example:

WebElement fxTable = driver.findElement(By.id("xRatesBxTable"));
List<WebElement> rows = fxTable.findElements(By.tagName("tr"));
int numTableRows= rows.size();
System.out.print(numTableRows);
 

By Link Text:

To locate a link element with matching visible text.

Selenium get link by text

Example:

driver.findElement(By.linkText("More currencies")).click();

 

By partial link text:

Helps locate a link element with partial matching visible text.

Selenium get link by text

Example:

driver.findElement(By.linkText("currencies")).click();

By CSS:

The CSS is used to identify the webobject, however NOT all browsers support CSS identification. In the above screenshot, we can get hold of div element with class moduleActionClass as shown below

Example:

driver.findElement(By.cssSelector("div.moduleActionClass"));

By XPath:

XPath stands for XML path language. It is a query language for selecting nodes from an XML document. We can make use of FirePath addon here to get an accurate XPath without a struggle.

Get XPath using FirePath

 

Example:

String val = driver.findElement(By.xpath(".//*[@id='amount']")).getAttribute("value");

 

In next chapter we will learn about Selenium WebDriver's Advanced User Interactions API >>>

 

 

 

 

Description

This tutorial will get you started with Selenium. This tutorial is subdivided into 11 chapters. 

  1. Overview
  2. IDE
  3. Environment Setup
  4. Remote Control
  5. Selenese Commands
  6. Webdriver
  7. Locators
  8. User Interactions
  9. Test design techniques
  10. TestNG
  11. Grid

 

 

 

* freelancer contributed



Environment

A PC capable of running selenium

Prerequisites

A basic idea of what Software Testing is.

Audience

People who wish to get started with Selenium

Learning Objectives

You will learn ins and outs of Selenium which includes downloading, installing and using Selenium.

Author: Subject Coach
Added on: 10th Feb 2015

You must be logged in as Student to ask a Question.

None just yet!