Wednesday 6 May 2015

Locators for Selenium Web Driver API



Locators for Selenium Web Driver API


Selenium web driver API uses eight locators to find the elements on web page. The below are the list of object identifier or you can say locators supported by selenium web driver API.
Prioritized the list of locators to be used while scripting.

1. Id, 2. Name, 3. Link text, 4. Partial Link text
5. Tag Name, 6. Class name, 7. CSS, 8. Xpath
Locating an Element By ID:
The most efficient way and preferred way to locate an element on a web page is By ID. ID must be the unique on web page which can be easily identified and effective.
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 id or email id which will be unique.
Example 1:
<div id="header">.....</div>
Example 2:
<input id="email" class="required" type="text"/>
We can write the scripts as below, second option is good for Ajax based application
WebElement Element = driver.findElement(By.id("header"));
or
WebElement submit=(new WebDriverWait(driver, 90))
  .until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='device_register_form']/div/div[3]/input")));
Unfortunately there are lot of case where an element does not contain a unique id (ids are dynamically generated). In these cases we need to go with an alternative locator formula. Locating an Element By Name:
When there is no Id to use, the next  approach to go with desired element has a name attribute. But make sure there the name must be unique all the times. If web pages has multiple names, Selenium web driver API  will always perform action on the first matching element
Example:
<input name="login" class="required" type="text"/>
WebElement registerobject= driver.findElement(By.name("login"));
Locating an Element By LinkText:
Finding an element with link text is very simple and effective. But quite 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 web driver API will execute on the first matching element with link.
Example:
<a href="seleniumhq.org">view</a>
WebElement downloadobject = driver.findElement(By.linkText("view"));
Locating an Element By Partial LinkText:
In the same way as LinkText, PartialLinkText also works in the same pattern.
Automation test engineer can provide partial link text to locate the element.
Example:
<a href="seleniumhq.org">Download selenium web driver API</a>
WebElement downloadobject = driver.findElement(By.PartialLinkText("Download"));
Locating an Element By TagName:
TagName can be used with Group elements like , Select and check-boxes / dropdowns.
the example code is below:
Select selectobject = new Select(driver.findElement(By.tagName("select")));
selectobject.selectByVisibleText("oct");
or
selectobject.selectByValue("11");
Locating an Element By Class Name:
There may be multiple elements with the same name, if we just use findElementByClassName and make sure it is only one. If not then you need to extend using the class name and its sub elements.
Example:
 
WebElement classtestobject =driver.findElement(By.className(“sample”));
 
CSS Selector:
CSS mainly used to provide style rules for the web pages and we can use for identifying one or more elements in the web page using css.
If you start using css selectors to identify elements, your test execution speed is good as compared with XPath.
Use CSS Selectors to make sure scripts run with the same speed in IE browser.
CSS selector is always the best possible way to locate complex elements in the page.
Example:
WebElement CheckElementsobject = driver.findElements(By.cssSelector("input[id=email']"));
XPath Selector:
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
Two types of xpath is present
1. Native Xpath, it is like eject location of element. Like below example.
Example:
html/head/body/table/tr/td
Here the advantage of specifying native path is, finding an element is very easy  to use. But there is one limitation (if something has been added/removed) then that xpath will no longer in use.
2. Relative Xpath.
In relative xpath we will be providing the relative path, it is like we will tell the xpath to find an element by telling the path in between.
Benefit here is, if at all there is any change in the html that works fine, until unless that particular location has changed.
Example:
//table/demo/tr(1)/td

No comments:

Post a Comment