Select & DeSelect values from Dropdownlist in WebDriver

Method Name: selectByVisibleText
Syntax: selectByVisibleText(String text)
Description: Select all options that display text matching the argument.. It will not look for any index or value, it will try to match the VisibleText (which will display in dropdown)
Example:
Select dropdown = new Select(driver.findElement(By.id(“language”)));
dropdown.selectByVisibleText(“Python”);

Method Name: selectByValue
Syntax: selectByValue(String value)
Description: Select all options that have a value matching the argument.
Example:
Select dropdown = new Select(driver.findElement(By.id(“language”)));
dropdown.selectByValue(“php”);

Method Name: selectByIndex
Syntax: selectByIndex(int index)
Description: Select the option at the given index.
Example:
Select dropdown = new Select(driver.findElement(By.id(“language”)));
dropdown.selectByIndex(3);

Method Name: deselectByVisibleText
Syntax: deselectByVisibleText(String text)
Description: Deselect all options that display text matching the argument.
Example:
Select dropdown = new Select(driver.findElement(By.id(“language”)));
dropdown.deselectByVisibleText(“Python”);

Method Name: deselectByValue
Syntax: deselectByValue(String text)
Description: Deselect all options that have a value matching the argument.
Example:
Select dropdown = new Select(driver.findElement(By.id(“language”)));
dropdown.deselectByValue(“php”);

Method Name: deselectByIndex
Syntax: deselectByIndex(int index)
Description: Deselect the option at the given index.
Example:
Select dropdown = new Select(driver.findElement(By.id(“language”)));
dropdown.deselectByIndex(3);

Method Name: deselectAll
Syntax: deselectAll()
Description: To Clear all selected entries.
Example:
Select dropdown = new Select(driver.findElement(By.id(“language”)));
dropdown.deselectAll();

Method Name: getOptions
Syntax: List getOptions()
Description: Get all options belonging to this select tag
Example:
Select dropdown = new Select(driver.findElement(By.id(“language”)));
List allOptions = dropdown.getOptions();
for (WebElement webElement : allOptions){
System.out.println(webElement.getText());
}

Method Name: getAllSelectedOptions
Syntax: List getAllSelectedOptions()
Description: Return all selected options belonging to this select tag
Example:
Select dropdown = new Select(driver.findElement(By.id(“language”)));
List allOptions = dropdown.getAllSelectedOptions();
for (WebElement webElement : allOptions){
System.out.println(webElement.getText());
}

Method Name: getFirstSelectedOption
Syntax: WebElement getFirstSelectedOption()
Description: Get the first selected option in this select tag (or the currently selected option in a normal select)
Example:
Select dropdown = new Select(driver.findElement(By.id(“language”)));
WebElement firstOption = dropdown.getFirstSelectedOption();System.out.println(firstOption.getText());

Method Name: isMultiple
Syntax: boolean isMultiple()
Description: Whether this select element support selecting multiple options at the same time? This is done by checking the value of the “multiple” attribute
Example:
Select dropdown = new Select(driver.findElement(By.id(“language”)));

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s