Count the number of elements in a dropdown list using Selenium
Steps:
1.Open web browser
2.Get the element of the dropdown list
3.Get the list elements using above webelement
4.Get the size of the list using size() method
import java.util.List;
import java.util.Set;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class CountNumberOfValues {
@Test
public void numberOfElementsDropDown() throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.monsterindia.com/");
Thread.sleep(4000);
String parentWindow = driver.getWindowHandle();
Set<String> windows = driver.getWindowHandles();
for(String window : windows) {
if(!parentWindow.equals(window)) {
driver.switchTo().window(window);
driver.close();
}
}
driver.switchTo().window(parentWindow);
Thread.sleep(3000);
driver.findElement(By.id("selExp_id")).click();
WebElement element = driver.findElement(By.cssSelector(".single_select"));
List<WebElement> elements = element.findElements(By.cssSelector(".option_item"));
int numberOfValues = elements.size();
System.out.println("The number of elements in the dropdown list "+numberOfValues);
driver.quit();
}
}
Steps:
1.Open web browser
2.Get the element of the dropdown list
3.Get the list elements using above webelement
4.Get the size of the list using size() method
import java.util.List;
import java.util.Set;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class CountNumberOfValues {
@Test
public void numberOfElementsDropDown() throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.monsterindia.com/");
Thread.sleep(4000);
String parentWindow = driver.getWindowHandle();
Set<String> windows = driver.getWindowHandles();
for(String window : windows) {
if(!parentWindow.equals(window)) {
driver.switchTo().window(window);
driver.close();
}
}
driver.switchTo().window(parentWindow);
Thread.sleep(3000);
driver.findElement(By.id("selExp_id")).click();
WebElement element = driver.findElement(By.cssSelector(".single_select"));
List<WebElement> elements = element.findElements(By.cssSelector(".option_item"));
int numberOfValues = elements.size();
System.out.println("The number of elements in the dropdown list "+numberOfValues);
driver.quit();
}
}
Ouput:
The number of elements in the dropdown list 52
No comments:
Post a Comment