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 a complete scenario.

Scenario to Automate:
1. Launch the web browser by creating a WebDriver object.
2. Visit the web page URL https://seleniumtesting08.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 see the following source code to understand the concept of deselection.
Program source 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) { WebDriver driver = new FirefoxDriver(); driver.manage().window().maximize(); String URL = "https://www.seleniumtesting08.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"); driver.close(); } }
    Output: Multiple choices are allowed, Select multiple options in the list Option May is deselected successfully
Explanations:
How to deselect dropdown option in Selenium WebDriver
As you can see in the above following 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.

Final words
Hope that this tutorial has covered the important points related to how to deselect dropdown in Selenium WebDriver. I hope that you will have understood how to deselect the option in the dropdown list and enjoyed the coding.
Thanks for reading!