FindElement And FindElements Methods In Selenium: In our previous post, we are trying to learn about the different Selenium WebDriver methods and commands. On which we have different types of elements on a web page. So to interact with the elements of the web page, we need to use the web element commands/actions. So to interact with those elements, we have to locate the element, and we can do that with the help of WebDriver instance.
Why do you need to FindElement And FindElements Methods?
To interact with the elements of the web page, we need to locate them first. So we can use the find element command for identifying the only one element from the web page. If we want to find multiple elements from the web page, then we can use the find elements commands. We can find the elements within the web page with the help of ID, Name, Class Name, Link Text, and Partial Link Text, Tag Name, and Xpath.
How to Locate Element Using findElement?
For locating the element, we are using the findElement method, which takes By object as the parameter and returns an object type of WebElement. With the By object, we can use the various locator strategies like ID, Name, Class Name, XPATH, etc. The Syntax of FindElement it looks something like this:
FindElement Syntax:
WebElement elementName = driver.findElement(By.LocatorStrategy("LocatorValue"));
The Locator value should be unique so that we can able to identify the elements uniquely. The responsibility of Developers or testers needs to generate the locator value so that it should uniquely identify the elements with the help of any locator strategies.
package com.selenium.practice.basic; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class FindElement_Example { public static void main(String[] args) throws InterruptedException { WebDriver driver; System.setProperty("webdriver.chrome.driver", "Path Of Browser driver File"); driver = new ChromeDriver(); driver.get("https://orangehrm-demo-6x.orangehrmlive.com"); driver.manage().timeouts().implicitlyWait(5000, TimeUnit.SECONDS); //Clearing The UserName Field Value driver.findElement(By.id("txtUsername")).clear(); Thread.sleep(5000); driver.close(); } }
How to Locate Element Using findElements?
As we have seen, find element method takes the by the object and returns a web element, and FindElements also takes the By object and returns a list of web elements. If FindElements were not able to locate elements, then it returns an empty list. the syntax for FindElements it is looking like below:
FindElements Syntax:
List<WebElement> elementName = driver.findElements(By.LocatorStrategy("LocatorValue"));
package com.selenium.practice.basic; import java.util.List; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class FindElements_Example { public static void main(String[] args) throws InterruptedException { WebDriver driver; System.setProperty("webdriver.chrome.driver", "Path Of Browser driver file"); driver = new ChromeDriver(); driver.get("https://testpages.herokuapp.com/basic_html_form.html"); driver.manage().timeouts().implicitlyWait(5000, TimeUnit.SECONDS); driver.manage().window().maximize(); //Clearing The UserName Field Value List<WebElement> li=driver.findElements(By.name("radioval")); Thread.sleep(5000); //Selecting the 1st element li.get(0).click(); Thread.sleep(5000); driver.close(); } }
Difference Between findElement & findElements
FindElement | FindElements |
---|---|
The Findelement we can find the first element within the current page using the “locating mechanism.” | With findElements, we can find all the matching elements within the current page using the “locating mechanism.” |
Find element return the First element or Single element | It will return a list of web elements. |
If It is not able to locate the element, then it will through NoSuchElementException | If it is not able to identify the elements, then it will not return any exception, it will return an empty list List<WebElement>. |
Leave a Reply