How To Handle Drop Down And Multi Select List Using Selenium WebDriver
Handle Drop Down And Multi Select List Using Selenium WebDriver:
To handle drop down and multi select list using Selenium WebDriver, we need to use Select class.
The Select class is a Webdriver class which provides the implementation of the HTML SELECT tag. It exposes several “Select By” and “Deselect By” type methods. We use these methods to select or deselect in the drop down list or multi select object. The Select class is the part of the selenium package.
We need to import the below mentioned library.
import org.openqa.selenium.support.ui.Select;
Standard syntax of Select Class is as follows:
Select dropdown = new Select(<WebElement>);
Example:
WebElement mySelectElement = driver.findElement(By.name("dropdown")); Select dropdown = new Select(mySelectElement);
or
Select dropdown = new Select(driver.findElement(By.xpath("//select[@name='dropdown']")));
Note: The Select class starts with capital ‘S’.
To Handle Drop Down And Multi Select List in Selenium we use the following types of Select Methods.
Types of Select Methods:
i. selectByVisibleText Method
ii. selectByIndex Method
iii. selectByValue Method
Types of DeSelect Methods:
i. deselectByVisibleText Method
ii. deselectByIndex Method
iii. deselectByValue Method
iv. deselectAll Method
Let’s see one by one with a sample program:
SelectByVisibleText Method:
It works based on the ‘visible text‘ provided by us.
Syntax:
dropdown.selectByVisibleText();
Sample program:
package softwareTestingMaterial; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.Select; import org.testng.annotations.Test; public class SelectMethod { @Test public static void captureScreenMethod() throws Exception{ System.setProperty("webdriver.chrome.driver","D://Selenium Environment//Drivers//chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("https://www.softwaretestingmaterial.com/sample-webpage-to-automate/"); driver.navigate().refresh(); //Once you got the select object initialised then you can access all the methods of select class. //Identify the select HTML element: Thread.sleep(10000); WebElement mySelectElement = driver.findElement(By.name("dropdown")); Select dropdown= new Select(mySelectElement); //To select an option - selectByVisibleText, selectByIndex, selectByValue //selectByVisibleText dropdown.selectByVisibleText("Automation Testing"); } }
SelectByIndex Method:
It works based on the ‘index value‘ provided by us.
Syntax
dropdown.selectByIndex(Index);
Sample program:
package softwareTestingMaterial; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.Select; import org.testng.annotations.Test; public class SelectMethod { @Test public static void captureScreenMethod() throws Exception{ System.setProperty("webdriver.chrome.driver","D://Selenium Environment//Drivers//chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("https://www.softwaretestingmaterial.com/sample-webpage-to-automate/"); driver.navigate().refresh(); //Once you got the select object initialised then you can access all the methods of select class. //Identify the select HTML element: Thread.sleep(10000); WebElement mySelectElement = driver.findElement(By.name("dropdown")); Select dropdown= new Select(mySelectElement); //To select an option - selectByVisibleText, selectByIndex, selectByValue //selectByIndex dropdown.selectByIndex(2); // value is QTP } }
SelectByValue Method:
It works based on the ‘value‘ provided by us.
Syntax
dropdown.selectByValue(Value);
Sample program:
package softwareTestingMaterial; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.Select; import org.testng.annotations.Test; public class SelectMethod { @Test public static void captureScreenMethod() throws Exception{ System.setProperty("webdriver.chrome.driver","D://Selenium Environment//Drivers//chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("https://www.softwaretestingmaterial.com/sample-webpage-to-automate/"); driver.navigate().refresh(); //Once you got the select object initialised then you can access all the methods of select class. //Identify the select HTML element: Thread.sleep(10000); WebElement mySelectElement = driver.findElement(By.name("dropdown")); Select dropdown= new Select(mySelectElement); //To select an option - selectByVisibleText, selectByIndex, selectByValue //selectByValue dropdown.selectByValue("ddmanual"); // value is Manual Testing } }
DeSelect Methods With Examples:
DeselectByVisibleText Method:
It works based on the ‘visible text‘ which we provide
Syntax:
dropdown.deselectByVisibleText();
Sample program:
package softwareTestingMaterial; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.Select; import org.testng.annotations.Test; public class SelectMethod { @Test public static void captureScreenMethod() throws Exception{ System.setProperty("webdriver.chrome.driver","D://Selenium Environment//Drivers//chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("https://www.softwaretestingmaterial.com/sample-webpage-to-automate/"); driver.navigate().refresh(); //Once you got the select object initialised then you can access all the methods of select class. //Identify the select HTML element: Thread.sleep(10000); WebElement mySelectElement = driver.findElement(By.name("multipleselect[]")); Select dropdown= new Select(mySelectElement); //To deselect an option //the deselect method will throw UnsupportedOperationException if the SELECT does not support multiple selections dropdown.selectByVisibleText("Performance Testing"); Thread.sleep(2000); dropdown.deselectByVisibleText("Performance Testing"); } }
DeselectByIndex Method:
It works based on the ‘index value’ which we provide
Syntax:
dropdown.deselectByIndex();
Sample program:
package softwareTestingMaterial; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.Select; import org.testng.annotations.Test; public class SelectMethod { @Test public static void captureScreenMethod() throws Exception{ System.setProperty("webdriver.chrome.driver","D://Selenium Environment//Drivers//chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("https://www.softwaretestingmaterial.com/sample-webpage-to-automate/"); driver.navigate().refresh(); //Once you got the select object initialised then you can access all the methods of select class. //Identify the select HTML element: Thread.sleep(10000); WebElement mySelectElement = driver.findElement(By.name("multipleselect[]")); Select dropdown= new Select(mySelectElement); //To deselect an option //the deselect method will throw UnsupportedOperationException if the SELECT does not support multiple selections dropdown.selectByIndex(2); Thread.sleep(2000); dropdown.deselectByIndex(2); } }
DeselectByValue Method:
It works based on the ‘value‘ provided by us.
Syntax:
dropdown.deselectByValue();
Sample program:
package softwareTestingMaterial; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.Select; import org.testng.annotations.Test; public class SelectMethod { @Test public static void captureScreenMethod() throws Exception{ System.setProperty("webdriver.chrome.driver","D://Selenium Environment//Drivers//chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("https://www.softwaretestingmaterial.com/sample-webpage-to-automate/"); driver.navigate().refresh(); //Once you got the select object initialised then you can access all the methods of select class. //Identify the select HTML element: Thread.sleep(10000); WebElement mySelectElement = driver.findElement(By.name("multipleselect[]")); Select dropdown= new Select(mySelectElement); //To deselect an option //the deselect method will throw UnsupportedOperationException if the SELECT does not support multiple selections dropdown.selectByValue("msagile"); Thread.sleep(2000); dropdown.deselectByValue("msagile"); } }
DeselectAll Method:
It is to deselect all the selected options at once
Syntax:
dropdown.deselectAll( );
Sample program:
package softwareTestingMaterial; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.Select; import org.testng.annotations.Test; public class SelectMethod { @Test public static void captureScreenMethod() throws Exception{ System.setProperty("webdriver.chrome.driver","D://Selenium Environment//Drivers//chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("https://www.softwaretestingmaterial.com/sample-webpage-to-automate/"); driver.navigate().refresh(); //Once you got the select object initialised then you can access all the methods of select class. //Identify the select HTML element: Thread.sleep(10000); WebElement mySelectElement = driver.findElement(By.name("multipleselect[]")); Select dropdown= new Select(mySelectElement); //To deselect an option //the deselect method will throw UnsupportedOperationException if the SELECT does not support multiple selections dropdown.selectByValue("msagile"); Thread.sleep(2000); dropdown.deselectAll(); } }
In order to get the selected option:
Sample program:
package softwareTestingMaterial; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.Select; import org.testng.annotations.Test; public class SelectMethod { @Test public static void captureScreenMethod() throws Exception{ System.setProperty("webdriver.chrome.driver","D://Selenium Environment//Drivers//chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("https://www.softwaretestingmaterial.com/sample-webpage-to-automate/"); driver.navigate().refresh(); //Once you got the select object initialised then you can access all the methods of select class. //Identify the select HTML element: Thread.sleep(10000); WebElement mySelectElement = driver.findElement(By.name("multipleselect[]")); Select dropdown= new Select(mySelectElement); WebElement option = dropdown.getFirstSelectedOption(); System.out.println(option.getText()); //output "Selenium" } }
In order to get the list of options from a dropdown:
Sample program:
package softwareTestingMaterial; import java.util.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.chrome.ChromeDriver; import org.openqa.selenium.support.ui.Select; import org.testng.annotations.Test; public class SelectMethod { @Test public static void captureScreenMethod() throws Exception{ System.setProperty("webdriver.chrome.driver","D://Selenium Environment//Drivers//chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("https://www.softwaretestingmaterial.com/sample-webpage-to-automate/"); driver.navigate().refresh(); //Once you got the select object initialised then you can access all the methods of select class. //Identify the select HTML element: Thread.sleep(10000); WebElement mySelectElement = driver.findElement(By.name("multipleselect[]")); Select dropdown= new Select(mySelectElement); List<WebElement> options = dropdown.getOptions(); for (WebElement option : options) { System.out.println(option.getText()); //output "Selenium", "QTP", "Manual Testing", "Automation Testing", "Performance Testing"*/ } } }
If you are not regular reader of my blog then I highly recommend you to signup for the free email newsletter using the below link.
Subscribe and get a free eBook and regular updates from SoftwareTestingMaterial.com
Hey bro..this is wonderful tutorial for basic to advanced 🙂 I duly appreicate your effort on making this. I have a request, could you please make videos Cucumber?
Thanks,
Narayana Venkatesh
Will try to do it Narayana.