How To Locate Web Elements In WebDriver

There are two methods to locate web elements in WebDriver as below.

findElement(By by): This method return one instance of WebElement class that match the by condition, you can use the returned object to interact with it. This method returns when the first matched one has been found. If no one has been found then a NoSuchElementException will be thrown.

findElemets(By by) : This method return a list of WebElement class’s instance that match the by condition. You can iterate the returned list and manipulate the objects in the list. This method returned only after the entire web page has be scanned. If no one match then return an empty list.

The input parameter(by) is the locator of how to locate web elements within a webpage. A typical usage of the input parameter(by) looks like below, you can find the detail information from the code comments.

 //Initiate a firefox webdriver object first.
 WebDriver fireFoxDriver = new FirefoxDriver();
 //Create a By locator to find the webelement using it's id.
 By byId = By.id("password");
 //Find the webpage element using the byId created above.
 WebElement passwordElement = fireFoxDriver.findElement(byId);

How to create the by locator to find webelement?

The By class in webdriver is used to create the web element locator. By class has 8 static methods that you can use to create the locator. You can see them in following picture.

Webdriver By class's static method

By.id(String id)
This can be the best and preferred method to locate a webelement, because IDs are usually unique. The String parameter is web element’s id attribute value. You can use the returned object to find the webelement whose id equal to the String parameter.

 //Initiate a firefox webdriver object first.
 WebDriver fireFoxDriver = new FirefoxDriver();
 //Create a By locator to locate the webelement using it's id. The webelement's id is "submit".
 By byId = By.id("submit");
 //Locate the webelement using the byId created above.
 WebElement submitElement = fireFoxDriver.findElement(byId);

inspect a web element's id in firebug

By.tagName(String name)
Locate the webpage elements which DOM tag name equals name parameter’s value.

 //Create a By locator to find the webpage element which DOM tag name is "button"
 By byTagName = By.tagName("button");
 //Find the webpage element using the byTagName created above.
 WebElement buttonElement = fireFoxDriver.findElement(byTagName);

inspect a web element's tagname in firebug

By.className(String className)
This is used to find web elements whose class attribute’s value equals the className parameter.

 //Create a By locator to find the element whose class attribute's value is "button"
 By byClassName = By.className("button");
 //Find the parent element using className locator above.
 WebElement pElement = fireFoxDriver.findElement(byClassName);
 //Use id value to find the child element under the parent element.
 WebElement cElement = pElement.findElement(By.id("submit"));
 //Submit the form.
 cElement.submit();

inspect a web element's class attribute value in firebug

By.name(String name)
This is used to find the web element whose name attribute’s value equals name parameter value.

  //Create a By locator to find the web element which name attribute's value is "firstname".
 By byName = By.name("firstname");
 //Find the web element using the by locator created above.
 WebElement firstNameElement = fireFoxDriver.findElement(byName);
 //Input "dev2qa.com" into the found text box.
 firstNameElement.sendKeys("dev2qa.com");

inspect a web element's name in firebug

By.xpath(String xpathExpression)
This is the most widely used and the easiest method to locate web element in WebDriver. The input parameter is a String value of xpath expression. Xpath provides a variety of method to locate web elements. You can locate a single web element in various ways.

 //Create a By locator to find the web element whose xpath is "//*[@id=\"post-439\"]/div/p[2]/a".
 By byXPath = By.xpath("//*[@id=\"post-439\"]/div/p[2]/a");
 //Find the web element using the by locator created above.
 WebElement xpathElement = fireFoxDriver.findElement(byXPath);

By.linkText(String linkText)
This is used to create a locator if you know the link text in html anchor(a) tag. The parameter is a string which is the exact value of link text in html anchor(a) tag. 

By.partialLinkText(String linkText)
This is like By.linkText(String linkText), the only difference is it can locate the html anchor(a) tag by part of the link text in the html anchor(a) tag.

 //Create a By locator use the link text in html anchor(a) tag. This need accurate whole string match.
 By byLinkText = By.linkText("Partial Link Test");
 //Create a By locator use part of the link text in html anchor(a) tag. This need only part string match.
 By byPartialLinkText = By.partialLinkText("Link Test");

inspect html anchor link text

Download “TestWebdriverFindElements.zip” TestWebdriverFindElements.zip – Downloaded 37 times – 907 B

(Visited 208 times, 1 visits today)
READ :   How to use Firefox webdriver to launch Firefox browser automatically in java

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.