Wednesday, July 25, 2012

How to locate Web Elements or UI Elements in a page

First of all inorder to locate the web elements we need to open or fetch (open) a web page. We can do this
by
 driver.get("http://www.google.com");


Web Elements:

Locating elements in WebDriver can be done on the WebDriver instance itself or on a WebElement. Each of the language bindings expose a “Find Element” and “Find Elements” method. The first returns a WebElement object otherwise it throws an exception. The latter returns a list of WebElements, it can return an empty list if no DOM elements match the query.
The “Find” methods take a locator or query object called “By”. “By” strategies are listed below.

By ID:
Ex:
< span=""> id="coolestWidgetEvah">... <>
Using Java:  
WebElement element = driver.findElement(By.id("coolestWidgetEvah"));
 



By Class Name:
Ex:
< span=""> class="cheese">Cheddar <>
< span=""> class="cheese">Gouda <>

Using Java:
List<WebElement> cheeses = driver.findElements(By.className("cheese")); 

By Tag Name:

The DOM Tag Name of the element.
Ex

No comments:

Post a Comment