- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Handling DropDown And Multiple Select in Webdriver using Selenium
We can handle multi-select dropdown with Selenium webdriver using the Select class. A multi-select dropdown is the one which allows selection of multi options.
The Select methods to handle multi-select dropdown are listed below −
getOptions – returns the list of all options in the dropdown.
Select s = new Select(e); List<WebElement> l = s.getOptions();
getFirstSelectedOption– returns the selected option in the dropdown. If there are multi options selected, only the first item shall be returned.
Select s = new Select(e); l = s. getFirstSelectedOption();
isMultiple – returns a boolean value, yields a true value if the dropdown allows selection of multiple items.
Select s = new Select(e); boolean l = s.isMultiple();
selectByIndex – The index of the option to be selected by the dropdown is passed as a parameter. The index starts from 0.
WebElement e = driver.findElement(By.id("slc")); Select s = new Select(e); s.selectByIndex(0);
selectByValue – The value attribute of the option to be selected by the dropdown is passed as a parameter. The options in the dropdown should have the value attribute so that this method can be used.
WebElement e = driver.findElement(By.id("slc")); Select s = new Select(e); s.selectByValue("option 1");
deselectByVisibleText – The visible text of the option to be deselected by the dropdown is passed as a parameter. It is only applicable to multi-select dropdowns.
WebElement e = driver.findElement(By.id("slc")); Select s = new Select(e); s.deselectByVisibleText("Selenium");
deselectByIndex – The index of the option to be deselected by the dropdown is passed as a parameter. The index starts from 0. It is only applicable to multi-select dropdowns.
WebElement e = driver.findElement(By.id("slc")); Select s = new Select(e); s.deselectByIndex(0);
deselectByValue – The value attribute of the option to be deselected by the dropdown is passed as a parameter. It is only applicable to multi-select dropdowns.
WebElement e = driver.findElement(By.id("slc")); Select s = new Select(e); s.deselectByValue("option 1");
selectByVisibleText – The visible text of the option to be selected by the dropdown is passed as a parameter.
WebElement e = driver.findElement(By.id("slc")); Select s = new Select(e); s.selectByVisibleText("Selenium");
deselectAll – unselects all selected options from the dropdown.
WebElement e = driver.findElement(By.id("slc")); Select s = new Select(e); s.deselectAll();
A multi-select dropdown has a select tag and its items are represented by the option tag.
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.support.ui.Select public class MultiDrpDwn{ public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); //implicit wait driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); //maximize browser driver.manage().window().maximize(); //URL launch driver.get("https://www.tutorialspoint.com/selenium/selenium_automation_practice.htm"); //identify dropdown WebElement d= driverfindElement(By.xpath("//select[@name='selenium_commands']")); //object of Select class Select s=new Select(d); //get options of dropdown in list List t =s.getOptions(); System.out.println("Options are: "); for (WebElement i: t){ System.out.println(i.getText()); } //return true if multi-select dropdown Boolean b=s.isMultiple(); System.out.println("Boolen value for drodown: "+ b); //select item by index s.selectByIndex(2); Thread.sleep(1000); //select item by visible text s.selectByVisibleText("Wait Commands"); Thread.sleep(1000); //get first selected option in dropdown WebElement f = s.getFirstSelectedOption(); System.out.println("First selected option is: "+ f.getText()); //deselect option by index s.deselectByIndex(2); Thread.sleep(1000); //deselect all selected items s.deselectAll(); driver.close(); } }
Output
- Related Articles
- How to Select Value from DropDown using Selenium Webdriver?
- Selenium WebDriver and DropDown Boxes.
- How to select an item from a dropdown list using Selenium WebDriver with java?
- What are the different ways to select an option from a dropdown using Selenium Webdriver?
- How to do session handling in Selenium Webdriver?
- How to select checkboxes using selenium java webdriver?
- How to get all options of a dropdown using Python Selenium webdriver?
- How to verify if we can select multiple options in a static dropdown in Selenium?
- How can I handle multiple keyboard keys using Selenium Webdriver?
- How to select an option in a static dropdown in Selenium?
- How will you select a particular value in a dropdown without using the methods of Select class in Selenium?
- Handling Browser Authentication using Selenium
- How can I select date from a datepicker div using Selenium Webdriver?
- How to Select date from a datepicker with Selenium Webdriver using Python?
- How to select the Date Picker In Selenium WebDriver?
