How to find web elements using FindElements Method?
March 11, 2020
How to find web elements using FindElements Method?
Welcome to this post!-“How to find web elements using FindElements Method?”
If you are looking for latest Selenium Webdriver concepts related to interview questions, then you are at right place. This post – “ How to find web elements using FindElements Method?” consists of discussion on FindElements method and a Complete Example.
Plus this post will give a broader perspective on the usage and implementation of FindElements using java in Selenium.
Usually these are assumed to be known in depth to check the logical solvency efficiency of the candidate and of course, suitability for the project and company. Go ahead and enjoy reading…
This discussion will help you prepare well if you are going for the interview or you need the function to be used in your project.
In the interview usually, you might be questioned in different ways like:
- How to identify many web elements of similar type using selenium?
- How can you locate input web elements on web application using selenium?
- How to index all the elements on web page using selenium?
- Can you use tagname to identify all link elements using selenium? If yes then how.
And so on.
Understanding FindElements Method
Till now we have seen identifying and locating a single element of any type whether it was input, text, link etc using various By class methods. But eventually we were dealing with findelement method which returns only one matching element.
Now question arises “if you have several checkboxes in a tables and you have to click all of them then how will you handle this with Selenium?”
Using Selenium WebDriver API which provides the findElements() method for acquisition of a list of elements say checkboxes based on matching the specified search criteria.
Example 1: Get all Checkboxes
Let’s see a complete example on this:
package automationFramework; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; public class MyClass { public static void main(String[] args) { // Create a new instance of the Firefox driver WebDriver driver = new FirefoxDriver(); // And now use this to visit website you want driver.get("http://www.yoururl.com"); // Alternatively the same thing can be done like this // driver.navigate().to("http://www.yoururl.com"); // Check the title of the page System.out.println("Page title is: " + driver.getTitle()) //xpath for checkbox -->In case you know the xpath” //string checkboxXPath = "//input[contains(@id, 'select')]"; //xpath for checkbox -->In case you don’t know the xpath” string checkboxXPath = "//input[@type='checkbox'] // Identify all checkboxes present List<WebElement> allCheckboxes = driver.FindElements(By.XPath(checkboxXPath)); //Click each checkbox and wait so that results are visible foreach(WebElement checkbox in allCheckboxes){ checkbox.Click(); System.Threading.Thread.Sleep(500);} //Close the browser driver.quit() } }
Another example, we can get all the links displayed on a page or get rows from a table, and so on.
Example 2: Get all the links
Let’s see a complete example on this:
package automationFramework; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; public class MyClass { public static void main(String[] args) { // Create a new instance of the Firefox driver WebDriver driver = new FirefoxDriver(); // And now use this to visit website you want driver.get("http://www.yoururl.com"); // Alternatively the same thing can be done like this // driver.navigate().to("http://www.yoururl.com"); // Check the title of the page System.out.println("Page title is: " + driver.getTitle()); //Get all the links displayed on Page List<WebElement> allLinks = driver.findElements(By.tagName("a")); //Verify there are eight links displayed on the page assertEquals(8, Links.size()); //Iterate though the list of links //print target for each link for(WebElement link : allLinks) System.out.println(link.getAttribute("href")); //Close the browser driver.quit(); } }
Now let us see some differences betwen FindElement and FindElements Method.
Difference between findElement and findElements
# | findElement | findElements |
Definition | driver.findElement() is used to find a webElement on a webpage. | driver.findElements() is used to find a List of webElements matching the locator passed as parameter. |
Syntax | WebElement ele = driver.findElement(By locator); | List manyEle = driver.findElements(By locator); |
For multiple matches | In case the multiple matches the findElement method returns the first web element found. | In case of multiple matches the findElements method returns a list of webElements. |
If no element is found | NoSuchElementException is thrown. | A List of 0 size is returned instead of an exception. |
Conclusion
Selenium WebDriver interface provides the findElements() method which traverse through the specified criteria and locates the web elements in the DOM for matching web elements of same type, and finally returns “all” matching element or Zero matched element as the case may be.
The most important aspect of Findelements method is that you can acquire child elements easily under any html tag.
This brings us to the end of our discussion on “How to find web elements using FindElements Method?”. Now we will see how we can find elements using css selectors?
I really hope you have enjoyed reading the post. If you have any doubt on this please feel free to add your comment below.
If you would like to keep track of further articles on Selenium. I recommend you to SUBSCRIBE by Email and have new Selenium articles sent directly to your inbox.