How to find size of a List of WebElements? How to find size of a List of WebElements? selenium selenium

How to find size of a List of WebElements?


You can use findElements() method and extract the size invoking the size() method as follows :

System.out.println(driver.findElements(By.xpath("//input[@name='group2']")).size());


Find the radio buttons by path and store them in a list

List<WebElement> radioGrp01 = driver.findElements(By.xpath("//input[@name= 'group1']"));System.out.println(radioGrp01.size());


WebDriver driver=new ChromeDriver();//WebDriver driver=new FirefoxDriver();driver.get("http://www.echoecho.com/htmlforms10.htm");//driver.findElement(By.xpath("//input[@value='Milk']")).click();int count =driver.findElements(By.xpath("//input[@name='group1']")).size();System.out.println(count);for(int i=0;i<count;i++){//driver.findElements(By.xpath("//input[@name='group1']")).get(i).click();String text=driver.findElements(By.xpath("//input[@name='group1']")).get(i).getAttribute("value");if(text.equals("Cheese")){driver.findElements(By.xpath("//input[@name='group1']")).get(i).click();}}