Automation QA Testing Course Content

Select Dropdown Options Using Select class in Webdriver

package:org.openqa.selenium.support.ui
Select:it is used to select an option(s) from single/multi selection dropdown
Models a SELECT tag, providing helper methods to select and deselect options.

1)identify the dropdown and assign it to a variable of type WebElement
WebElement drp=driver.findElement(By.locator("locatorvalue"));

2)create an object for a Select class & pass the element reference to the select class object
//classname refvar=new classname(WebElement element);
Select sel=new Select(drp);

1)select an option from dropdown by label text
sel.selectByVisibleText("labeltext");

2)select an option by value attribute
sel.selectByValue("value");

3)select an option by position
sel.selectByIndex(int index);

4)fetch all the dropdown options into collection
List<WebElement>opts=sel.getOptions();
//selecting last option from dropdown
sel.selectByIndex(opts.size()-1);

5)deselect all the options from multiselection dropdown
sel.deselectAll();

6)deselect an option by label text
sel.deselectByVisibleText("labeltext");
7)deselect an option by value attribute
sel.deselectByValue("value");
8)deselect an option by position
sel.deselectByIndex(int index);

9)fetch all the selected options from multiselection dropdown
List<WebElement>slctdopts=sel.getAllSelectedOptions();

10)fetch the first selected option from dropdown
WebElement frstslctdopt=sel.getFirstSelectedOption();

11)isMultiple():to know whether the given dropdown is single or multiselection dropdown

if(sel.isMultiple()){
write here multiselection dropdown code;
}else{
single selectiondropdown code;
}

Note:Throws:
UnexpectedTagNameException - when element is not a SELECT

Throws:
NoSuchElementException - If no matching option elements are found

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.