How to search for the required option in the List box using Selenium WebDriver

05 April, 2014
To search for the required option in the List box using Selenium WebDriver, we can use the below code

Read here about how to handle the List Box (Drop Down List and Multi-Select List).


import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class A_07_Print_all_Options_ListNBox {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = new FirefoxDriver();
	driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
	driver.get("http://fatcow.com/");
	String status = "Not Found";
	WebElement listBox = driver.findElement(By.id("countrySelect"));
	Select select = new Select(listBox);
			
	List allOptions = select.getOptions();
	int size = allOptions.size();
	for (int i = 0; i < size; i++) {
		String s = allOptions.get(i).getText();
		if (s.equals("Canada")) {
			status= "Found";
			break;		
		}
	}	
	System.out.println(status);
    }	
}

No comments:

Post a Comment