[Selenium WebDriver Videos]: How to verify drop down listbox values

Опубликовано: 20 Август 2026
на канале: Udaya Kumar Anem
9,556
31

Watch my below blog for complete ordered list of Selenium WebDriver topis:

http://seleniumwebdrivervideos.blogsp...

In this video i demonstrated about how to verify dropdown listbox values.

Below is the code which i used for demonstration:
public class ValidateDropDownListboxValues
{
public static void main(String[] args)
{
//WebDriver instance
WebDriver driver=new FirefoxDriver();
//Manage implicit wait
driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
//Navigate to the URL
driver.get("E:\\Programming Samples\\HTML Samples\\DropDownListBox.html");
//Instantiate the WebElement
WebElement carsDropDownList=driver.findElement(By.id("sampleListBox"));
//Create a Select object with the element
Select selectCarsDropdown=new Select(carsDropDownList);
List <WebElement> allOptions=selectCarsDropdown.getOptions();

String cars="Select a Car...;Benz A Class;BMW X3;Audi A4;Porsche Carrera123;Rolls Royce Ghost;Bugatti Type 5";
String[] arrCars=cars.split(";");

for(String str:arrCars)
{
boolean found=false;
for(WebElement ele:allOptions)
{
if(str.equals(ele.getText()))
{
found=true;
System.out.println(str+" Option value exists");
break;
}
}
if(!found)
{
System.out.println(str+" Option value does not exists");
}
}
driver.close();
}
}