Working with Locators
Every webpage or website comprises of several HTML and CSS elements, which constitute the DOM structure. These elements in selenium are known as WebElements and WebDriver provides us with various feature to locate any WebElement uniquely in our AUT. These WebElements are also referred as locators in selenium.
findElement(By) and findElements(By) are two of the WebDriver methods which are used to locate any WebElement. findElement() returns exact match if the WebElement is found in DOM, whereas latter returns a list of WebElements matching the locator type. By is the strategy employed by WebDriver to identify a locator. Following are the various By strategies using which we can identify a WebElement.
id is the most widely used strategy, given that it is uniquely identified in AUT. This is the fastest technique to locate any WebElement.
Example how to use id to locate a WebElement:
<div id="myID">...</div> WebElement element = driver.findElement(By.id("myID"));
Here element is a variable of type WebElement(every object in a webpage is considered as WebElement in Selenium), which is used to hold the object returned by findElement.
A driver is an instance of WebDriver, which is used to drive the browser and finally By.id is the strategy with which we located a WebElement with id value myID.
<div class="automation"> <span>QTP</span></div> <div class="automation"> <span>Selenium</span></div>
In the above example, the class name of 2 WebElements is same “automation”. If we use By.className strategy then it will return us 2 objects of “automation”. To handle multiple objects we will use findElements method.
List<WebElement> elements = driver.findElements(By.className(“automation”));
Every HTML has a tag associated with it. We can use this tag to locate our WebElement.
<a src= “www.xyz.com/” ></a>
Here “a” tag signifies the anchor tag, which represents a link in a webpage. A website may have multiple links in it, hence we will again use findElements method to extract multiple anchor tag objects and store them in a List of type WebElement.
List<WebElement> anchorTag = driver.findElements(By.tagName(“a”));
We can use this strategy if it identifies a WebElement uniquely.
<input name="automation" type="text"/> WebElement nameLocator = driver.findElement(By.name(“automation”));
We can locate a link using the text it displays.
<a href="http://www.XYZ.com">automation</a> WebElement linkText = driver.findElement(By.linkText(“automation”));
This is an extension of the above strategy. We will use this technique if our link text is very long and we do not want to enter full link text. partialLinkText will return all the WebElements which matches the text of the link.
<a href="http://www.XYZ.com">automation using Selenium</a> WebElement linkText = driver.findElement(By.partialLinkText(“automation”));
There are two other popular ways to identify a locator:
xpath is surely the best technique to locate a web element but it is relatively slow as compared to cssSelector.