Tuesday 4 August 2015

How to use locators in Selenium Webdriver using java

Selenium WebDriver use locators to find the elements on web page 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 or throw an exception if it does not find any element matching the search criteria.
findElements() returns a list of WebElements matching the search criteria. If no elements are found, it returns an empty list.
The locators elements of Selenium Webdriver as run as following with java syntax and example.
1. id
2. class name
3. Linktext
4. Partial Linktext
5. Tag Name
6. Name
7. Css
8. xpath

Method: By ID
Syntax: driver.findElement(By.id(<element ID>))
Description: Locates an element using the ID attribute. The most efficient way and preferred way to locate an element on a web page is By ID. ID will be the unique on web page which can be easily identified. IDs are the safest and fastest locator option and should always be the first choice even when there are multiple choices, It is like an Employee Number or Account which will be unique.
Example:
<input type="text"  id="email" name="email" class="inputtext">
<div id="main_menu">Home.</div>
Selenium Webdriver write as:
WebElement loginemail= driver.findElement(By.id("email"));
String menuname= driver.findElement(By.id("main_menu")).getText();
driver.findElement(By.id("email")).sendKeys("abced@gmail.com");

Method: By class name
Syntax: driver.findElement(By.className(<element class>)), driver.findElements(By.className(<element class>))
Description: Locates an element using the Class attribute. There may be multiple elements with the same name. We use findElement() method for single element class name. We use findElements() for  multiple elements with the same name.
Example:
<input type="text"  id="email" name="email" class="inputtext">
<input type="password" id="pass" name="pass" class="inputtext">
or
<input name="register" class="required" type="text"/>
<div class="mobile">8801911444444.</div>
Selenium Webdriver write as:
List<WebElement> noinputf = table.findElements(By.className("inputtext"));
or
WebElement loginemail= driver.findElement(By.className("required"));
WebElement loginemail= driver.findElement(By.className("mobile")).getText();

Method: By link text
Syntax: driver.findElement(By.linkText(<linktext>))
Description: Locates a link using link text. Make sure, there is only one unique link on the web page. If there are multiple links with the same link text (such as repeated header and footer menu links), in such cases Selenium will perform action on the first matching element with link.
Example:
<a href="http://selenium-release.storage.googleapis.com/2.45/selenium-java-2.45.0.zip">Download</a>
<a href="/projects/">Selenium Projects</a>
Selenium Webdriver write as:
WebElement download = driver.findElement(By.linkText("Downloads"));
driver.findElements(By.linkText("Selenium Projects")).click();

Method: By partial link text
Syntax: driver.findElement(By.partialLinkText(<linktext>))
Description: Locates a link using the link's partial text. It is same as By link text
Example:
<a href="http://selenium-release.storage.googleapis.com/2.45/selenium-java-2.45.0.zip">Download</a>
<a href="/projects/">Selenium Projects</a>
Selenium Webdriver write as:
WebElement download = driver.findElement(By.partialLinkText("Downloads"));
driver.findElements(By.partialLinkText("Selenium")).click();

Method: By tag name
Syntax: driver.findElement(By.tagName(<htmltagname>))
Description: Locates an element using the HTML tag. It is very easy to handle tables,Select, and check-boxes / drop downs etc  with the help of this method.
Example:
<table style="width:100%" id="data">
  <tr>
    <td>Jill</td>
    <td>Smith</td>
    <td>50</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td>
    <td>94</td>
  </tr>
</table>

<a href="http://selenium-release.storage.googleapis.com/2.45/selenium-java-2.45.0.zip">Download</a>
<a href="/projects/">Selenium Projects</a> 
Selenium Webdriver write as:
WebElement table = driver.findElement(By.id("data"));
List<WebElement> row = table.findElements(By.tagName("tr"));

List<WebElement> links= driver.findElements(By.tagName("a"));
 
Method: By name
Syntax: driver.findElement(By.name(<element name>))
Description: Locates an element using the Name attribute. Name attribute can't be unique all the times. If there are multiple names, Selenium will always perform action on the first matching element.
Example:
<input type="text"  name="email" class="inputtext">
Selenium Webdriver write as:
WebElement email= driver.findElement(By.name("email"));

Method: By CSS
Syntax: driver.findElement(By.cssSelector(<css selector>))
Description: Locates an element using the CSS selector. The CSS is used as a method to provide style rules for the web pages and we can use for identifying one or more elements in the web page using css.
Example:
WebElement login = driver.findElement(By.cssSelector("input.login"));

Method: By XPath
Syntax: driver.findElement(By.xpath(<xpath>))
Description: Locates an element using XPath query. XPath is designed to allow the navigation of XML documents, with the purpose of selecting individual elements, attributes, or some other part of an XML document for specific processing.
Example:
driver.findElement(By.xpath("html/head/body/table/tr/td"));

No comments:

Post a Comment