02_ Coding: Drop Down

The 'Select' class in Selenium WebDriver is used for selecting and deselecting option in a dropdown. The objects of Select type can be initialized by passing the dropdown webElement as parameter to its constructor.

WebElement dropdownElement = driver.findElement(By.id("flightSearchForm.adultOrSeniorPassengerCount"));

Select select = new Select(dropdownElement);

1. selectByIndex - It is used to select an option based on its index, beginning with 0.

select.selectByIndex(3);

2. selectByValue - It is used to select an option based on its 'value' attribute.

select.selectByValue("value");

3. selectByVisibleText - It is used to select an option based on the text over the option.

select.selectByVisibleText("valibleText");

#1)selectByVisibleText() and deselectByVisibleText()

#2) selectByValue() and deselectByValue()

#3) selectByIndex() and deselectByIndex()

#4) isMultiple()

#5) deselectAll()

Single selected options

public void dropDown() {

driver = new ChromeDriver();

driver.get("https://www.aa.com/homePage.do");

WebElement dropdownElement= driver.findElement(By.id(" xyz"));

Select select = new Select(dropdownElement);

select.selectByIndex(3);



Multiple selected options

Method :1

// Get all selected options

driver = new ChromeDriver();

driver.get("https://hamropicture.w3spaces.com/drop-down.html");

WebElement dropdownElement = driver.findElement(By.xpath("//*[@id='selectmultipleoption']"));

Select select = new Select(dropdownElement);

// make sure it is allow multiple selection if this condition is true

select.isMultiple();

// one way to select multiple element from drop down

select.selectByIndex(0);

select.selectByIndex(1);

select.selectByIndex(2);

select.selectByIndex(3);

select.deselectAll();


Method :2

driver = new ChromeDriver();

driver.get("https://hamropicture.w3spaces.com/drop-down.html");

WebElement dropdownElement = driver.findElement(By.xpath("//*[@id='selectmultipleoption']"));

Select select = new Select(dropdownElement);

// make sure it is allow multiple selection if this condition is true

select.isMultiple();

List<WebElement> list = select.getOptions();

for (WebElement a : list) {

select.selectByVisibleText(a.getText());


********************************************************************************

Note :

getOption() method get all option from select's options 

List<WebElement> alloptionlist = select.getOptions();

getAllSelectedOption() method get onlySelected option from select's options 

List<WebElement> list = select.getAllSelectedOptions()












Comments

Popular posts from this blog

Important link