How to Deselect Dropdown Value in Selenium WebDriver

Deselect dropdown value in Selenium | We know that Selenium WebDriver supports the testing of dropdown elements using a special select class.

Select class in selenium provides various methods to select and deselect dropdown elements. In this tutorial, we will learn how to deselect dropdown options in selenium when the dropdown option already selected.

Let’s create a test in which we will perform some basic checks, and will then call various methods to select and deselect the options from the dropdown list. So, let us look at a glance complete scenario.

Scenario to Automate:

1. Launch the web browser by creating a WebDriver object.

2. Visit the web page URL (https://selenium08.blogspot.com/2019/11/dropdown.html).

3. Locate the dropdown element on the web page by By.xpath.

4. Verify that dropdown supports multiple-choice or not.

5. Select multiple options by using methods selectByVisibleText(), selectByValue(), selectByIndex() of select class.

6. Deselect already selected option by using deselectByVisibleText() method of select class.

Let’s create a program to automate the above scenarios. Look at the following program code to understand the concept of deselection.

Program code:

package seleniumProject; 
import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.support.ui.Select; 

public class DeselectOptionExample { 
public static void main(String[] args) 
{ 
 // Create an object of Firefox driver class by using the reference of WebDriver.
    WebDriver driver = new FirefoxDriver(); 
    driver.manage().window().maximize(); 
  
    String URL = "https://selenium08.blogspot.com/2019/11/dropdown.html"; 
    driver.get(URL); 

 // Wait for some time to load the whole web page. 
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 

 // Locate dropdown element on the web page by By.xpath. 
    WebElement dropdown = driver.findElement(By.xpath("//select[@name='Month']")); 

 // Create an object of Select class and pass the dropdown of type WebElement as an argument. 
    Select select = new Select(dropdown); 

 // Verify that the dropdown supports multiple-choice or not.. 
    if(select.isMultiple()) 
    { 
        System.out.println("Multiple choices are allowed, Select multiple options in the list "); 
    } 
    else { 
        System.out.println("Multiple choices are not allowed"); 
    } 
 // Select the option "India" by sending visible text. 
    select.selectByVisibleText("May"); 

 // Select an option by sending its value. 
    select.selectByValue("Ap"); 

 // Select an option by sending its index. 
    select.selectByIndex(10); 

 // Now, we will deselect already selected option by calling deselectByVisibleText() method of the select class. 
    select.deselectByVisibleText("May"); 
    System.out.println("Option May is deselected successfully"); 
   
 // Closing the web browser.
    driver.close(); 
  } 
}
Output: 
       Multiple choices are allowed, Select multiple options in the list 
       Option May is deselected successfully

Explanation:

How to deselect option in dropdown list

As you can see in the above following source code, we have selected three options using three different methods of select class in the dropdown list. Out of three selected options, we deselected one option by using a method deselectByVisibleText() method of select class.

You can also see in the above screenshot, only two selected options are shown after the deselection of one option in the multiple-choice dropdown list.


In this tutorial, we have performed an automation task on how to deselect dropdown value in Selenium WebDriver. Hope that you will have understood how to deselect the option in the dropdown list and enjoyed the coding. In the next, we will learn how to handle alert in Selenium WebDriver with the help of examples.
Thanks for reading!!!

⇐ PrevNext ⇒

Please share your love