Search On Google

Saturday, February 16, 2019

Selenium findElement vs findElements

What is findElement?

Find element method is use to find single element by using any locators id, name etc

Webelement element= driver .findElement(By.id("id_1"));

What is findElements?

findElements is method is use to find multiple elements by using  locator  id, name ,Xpath  etc into a list.
This method mostly work with name and xpath locator because in some cases we have multiple element with same name.
In case of radio button multiple radio button use same name for grouping.

List<Webelement> elementsList1 = driver .findElements(By.id("radio_buttons"));
List<WebElement> elementsList2 =  driver.findElements(By.xpath("//div"));

JAVA Code Example for findElements:-


"package pkg1;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.*;

public class MyClass7_findElements_and_findElement { 
    public static void main(String[] args) {       
   
        System.setProperty(""webdriver.chrome.driver"",""C:\\chromedriver.exe"");   
        WebDriver driver = new ChromeDriver();   

        driver.get(""https://www.onlinetutorial.info/p/selenium-findelement-vs-findelements.html"");   
   
        List<WebElement> elements = driver.findElements(By.name(""name""));
        System.out.println(""Number of elements:"" +elements.size());

        for (int i=0; i<elements.size();i++){
          System.out.println(""Radio button text:"" + elements.get(i).getAttribute(""value""));
        }
    }
}

No comments:

Post a Comment

About Me

My photo
Mumbai, Maharashtra, India