☰ See All Chapters |
How to locate/find elements in selenium WebDriver
In this tutorial you will learn to use the locators in web driver. In our previous tutorial of Locators in Selenium, we learn different types of locators in selenium and also we learn to use them in selenium IDE. Now we will use the same locators in web driver. It is Important to note that the locators we learn in our previous chapter are same here in WebDriver, Locating elements is done by using the “findElement()” and “findElements()” methods.
We have 8 types of locators in Selenium using which findElement()/findElements() methods identifies elements on the web page:
id
name
tagName
className
linkText
partialLinkText
xpath
cssSelector
We learnt to use these locators with selenium IDE. To use these locators in webdriver we have By class having the factory methods for each of the locators. A method which returns the instance of the same class in which it is present is called as Factory method. All the locators are factory method which returns an instance of By class.
WebDriver driver = new FirefoxDriver(); driver.findElement(By.id("firstName")); driver.findElement(By.name("lastName")); driver.findElement(By.tagName("select")); driver.findElement(By.class("datePicker")); driver.findElement(By.linkText("Welcome to www.tools4testing.com")); driver.findElement(By.partialLinkText("java")); driver.findElement(By.xpath("//*[@id="firstname"]")); driver.findElement(By.cssSelector("#firstName")); |
</head> <body> <form name="signup" id="form" modelAttribute="data"> <table align="center" width=90% cellspacing="2" cellpadding="2"> <tr> <td>First Name :</td> <td><input type="text" id="firstname"></td> </tr> <tr> <td>Last Name :</td> <td><input type="text" name="lastname"></td> </tr> <tr> <td>Date of Birth :</td> <td><input type="text" class="datePicker" placeholder="dd-mm-yyyy"></td> </tr> <tr> <td>Gender :</td> <td><select> <option value=""></option> <option value="Male">Male</option> <option value="Female">Female</option> <option value="Transgender">Transgender</option> </select></td> </tr> <tr> <td><a href="#">Welcome to www.tools4testing.com</a></td> </tr> <tr> <td><a href="#">Welcome to www.java4coding.com</a></td> </tr> <tr> <td colspan="2"> <p align="center"> <input type="button" value="Submi" name="submitUpdate" onclick="onclickSubmit()" /> </td> </tr> </table> </form> </body> </html> |
All the methods of By class accepts a sting value, this value should be the locator value.
The “findElement()” method returns the first WebElement object based on a specified search criteria, or throws an exception, if it does not find any element matching the search criteria. The “findElements()” method returns a list of WebElements matching the search criteria. If no elements are found, it returns an empty list. The “findElement()” and “FindElements()” methods throw a “NoSuchElementFoundException” exception when they fail to find the desired element using the specified locator strategy.
Determine if an element exists without throwing an exception
The “findElement()” and “FindElements()” methods throw a “NoSuchElementFoundException” exception when they fail to find the desired element using the specified locator strategy. You must handle this exception. Sometime pages may not be loaded with desired element which we expect and execution halts due to the exception.
To get around this, you can use “findElements()”, and check that the size of the list returned is 0.
Example:
List<WebElement> elements = driver.findElements(By.Id("testElement"));
elements.size(); //If this is not zero, then element exists
Please click on the below Locator tutorial which you are interested in reading. If you have not read any of these, we suggest reading all these in the order we provide without skipping any.
You can use below test pages while learning these locators.
All Chapters