How to select all the options present in Multi-Select List box using Selenium WebDriver

04 April, 2014
We can use the below code to select all the options present in multi-select list box.

Read here about how to handle the List Box (Drop Down List and Multi-Select 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 MultiSelect {

	public static void main(String[] args) throws InterruptedException {
		WebDriver driver = new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
		
		driver.get("http://fatcow.com/");
		WebElement listBox = driver.findElement(By.id("countrySelect"));
		Select select = new Select(listBox);
		
		int size= select.getOptions().size();
		System.out.println("Number of Options: " +size);
		
		for (int i = 0; i < size; i++) {
			select.selectByIndex(i);
		}
	}
	
}

No comments:

Post a Comment