A Guide For Understanding: FindElement VS FindElements In Selenium
At whatever point you intend to automate any web application utilizing Webdriver, you normally start by finding the HTML components on the page. At that point, you grade to pick the correct system to find them. Webdriver characterizes two techniques for distinguishing the components; they are findElement and findElements.
A client is needed to collaborate with a site page to find the web component. The command Find Element in Selenium is utilized to exceptionally find a web component inside the page. While the command FindElements in Selenium is utilized to find the rundown of web components inside the website page particularly. There are numerous approaches to distinguish a web component inside the site page like Name, ID, Link Text, Partial Link Text, ClassName, XPath, and Tag Name. Now let’s discuss the major differences between Selenium findElement and findElements.
FindElement
- Each time when we need to perform any procedure on the WebElement findElement method is used.
- If findElement didn’t find any WebElement on-page, It throws a NoSuchElementException exception when it fails to discover the component.
- Utilizing the findElement method we discover WebElement from WebPage and perform Operations on it.
- findElement method returns just first/Single WebElement.
FindElement in Selenium takes in the By object as the parameter and returns an object of type list WebElement in Selenium. By object can be utilized with different locator systems, for example, discover components by ID Selenium, Name, Class Name, XPATH, etc. The following is the syntax structure of FindElement in the Selenium web driver.
Syntax:
driver.findElement(By.xpath(“Value of Xpath”));
Locator Strategy can be any of the following qualities.
- ID
- Selenium find element by Name
- Class Name
- Tag Name
- Link Text
- Halfway Link Text
- XPATH
Locator Value is the unique value utilizing which a web component can be distinguished. Developers and testers must ensure that web components are extraordinarily recognizable utilizing certain properties like ID or name.
FindElement Method
1234567891011121314151617181920 | package seleniumTutorial;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.firefox.FirefoxDriver;public class FindelementFindElements {public static void main (String [] args){// Open browserWebDriver driver = new FirefoxDriver();// To maximize the windowdriver.manage.window.maximize();// Open Applicationdriver.get(“https://www.google.co.in/?gws_rd=ssl#q=softwaretestingmaterial.com”);// Get text of a particular linkStringFindElement = driver.findElement(By.xpath(“//*[@id=’rso’]/div[1]/div/div/h3/a”)).getText();// Print the value of the linkSystem.out.println(FindElement);// Click on the linkdriver.findElement(By.xpath(“//*[@id=’rso’]/div[1]/div/div/h3/a”)).click();}} |
Let’s get an insight into the usage of Selenium findElement in various ways:
1. By Name
Command: driver.findElement(By.name(<element-name>))
Example: <input name=”JournalDev”>
Java example code to find the input element by name
Syntax:
WebElement user = driver.findElement(By.name(“JournalDev”));
2. By Class Name
Command: driver.findElement(By.className(<element-class>))
Example: <input class=”JournalDev”>
Java example code to find the input element by className.
Syntax:
WebElement user = driver.findElement(By.className(“JournalDev”));
3. By CssSelector
Command: driver.findElement(By.cssSelector(<css-selector>))
Example:
<input class=”email” id=”email” type=”text” placeholder=”xxx@email.com”>
<input class=”btn btn-small” type=”submit” value=”Subscribe to blog>
Java example code to find element matching link or partial link text:
Syntax:
WebElement emailText = driver.findElement(By.cssSelector(“input#email”));
4. By ID
Command: driver.findElement(By.id(<element ID>))
Example: <input id=”JournalDev”>
Java example code to find the input element by id
Syntax:
WebElement user = driver.findElement(By.id(“JournalDev”));
5. By LinkText
Command: driver.findElement(By.linkText(<link text>))
Example:
<a href=”#test1″>JournalDev-1</a>
<a href=”#test2″>JournalDev-2</a>
Java example code to find element matching link or partial link text:
Syntax:
WebElement link = driver.findElement(By.linkText(“JournalDev-1”));
WebElement link = driver.findElement(By.partialLinkText(“JournalDev-2”));
6. By XPath
Command: driver.findElement(By.xpath(<xpath>))
Java example code for XPath:
// Absolute path
WebElement item = driver.findElement(By.xpath(“html/head/body/table/tr/td”));
// Relative path
WebElement item = driver.findElement(By.xpath(“//input”));
// Finding elements using indexes
WebElement item = driver.findElement(By.xpath(“//input[2]”));
FindElements
- At the point when we need to get numerous WebElement then we use findElements for eg: Fetch all DropDown list records to confirm the record is available or not in the DropDown List.
- If not found any WebElement on the current page according to the given component locator instrument, it will restore the void list.
- For this, the list of WebElements will be returned, we need to give the locator in such a way that it can look for some components and return the list of web components through the rundown, we can emphasize it and do our activity.
Syntax:
List link = driver.findElements(By.xpath(“Value of Xpath”));
FindElements Method
12345678910111213141516171819202122232425 | package seleniumTutorial;import java.util.List;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.firefox.FirefoxDriver;public class FindelementFindElements {public static void main (String [] args){// Open browserWebDriver driver = new FirefoxDriver();// To maximize the windowdriver.manage().window().maximize();// Open applicationdriver.get(“https://www.google.co.in/?gws_rd=ssl#q=softwaretestingmaterial.com”);// Get the list of all linksList link = driver.findElements(By.xpath(“//*[@id=’rso’]/div/div/div/h3/a”));// for loop which will display the text of all the linksfor(WebElement element:link){System.out.println(element.getText());}// Click on the first linkdriver.findElement(By.xpath(“//*[@id=’rso’]/div/div/div/h3/a”)).click();}} |
Difference Between FindElement and FindElements
findElement | findElements |
It returns just the primary component when numerous components are discovered utilizing the same locator. | Return all components from a rundown of web components. |
Return single element | Return a list of elements |
If no coordinating component is discovered, toss NoSuchElementFound Exception. | If no coordinating component is discovered, it will restore an empty list. |
Apply to discover component method over component found. | Unrealistic to apply |
Not Applicable | Each Web Element is ordered with a 0 number much the same as an array. |
Throws exception NoSuchElementException if no components are coordinating the locator system. | Returns an unfilled list if no web components are coordinating the locator system. |
Discover component by XPath will just discover one web component. | It will discover an assortment of components that coordinate the locator system. |
Our Popular Articles