GroTechMinds

deselecting the selected option using selenium

Deselecting the Selected Options Using Selenium

Introduction

With the help of the ‘Select’ class, we can select one of the options from the drop-down menu. What if, there was a multi-select box? How can we select multiple options from the box with a ‘select’ tag? Let us dive in and find out.

Multi-Select Box

Handling a multi-select box is tricky, and Selenium has two sets of methods for selecting and deselecting the options of the multi-select box. Multi-select boxes come in handy for surveys and forms where multiple preferences are to be received, filtering search results, etc., A word of caution is that if we use the methods for deselecting like ‘deselectAll()’ for a regular drop-down menu, we will get “UnsupportedOperationException”.

Selecting the options in the multi-select box
  1. To make a selection based on the index position of the option in the list, we use a method of the ‘Select’ class called ‘selectByIndex(int index)’
  2. To make a selection based on the ‘value’ attribute name’s attribute value of the option in the list, we use a method of the ‘Select’ class called ‘selectByValue(String value)’
  3.  To make a selection based on the String value (textual representation) of the option in the list, we use a method of the ‘Select’ class called ‘selectByVisibleText(String text)’

Code Snippet:

				
					public static void main(String[] args)
{
WebDriver driver= new ChromeDriver();
driver.get("https://grotechminds.com/multiple-select/");
WebElement dropdown= driver.findElement(By.id("automobiles"));
Select sel=new Select(dropdown);
sel.selectByIndex(0);
sel.selectByValue("sedan");
sel.selectByVisibleText("Hatchback");
sel.selectByIndex(3);
}

				
			

After Selection:

w(1)
Deselecting the selected options in the multi-select box

After selecting the desired options in the multi-select box, using the methods from the ‘Select’ class, we can deselect the selection using the following methods.

  1. To deselect a selection based on the index position of the option in the list, we use a method of the ‘Select’ class called ‘deselectByIndex(int index)’

Code Snippet:

				
					public static void main(String[] args) throws InterruptedException {
WebDriver driver= new ChromeDriver();
driver.get("https://grotechminds.com/multiple-select/");
WebElement dropdown= driver.findElement(By.id("automobiles"));
Select sel=new Select(dropdown);
sel.selectByIndex(0);
sel.selectByValue("sedan");
sel.selectByVisibleText("Hatchback");
sel.selectByIndex(3);
sel.deselectByIndex(0);
sel.deselectByIndex(3);
}

				
			

After deselection:

l(1)
  1. To deselect a selection based on the ‘value’ attribute name’s attribute value of the option in the list, we use a method of the ‘Select’ class called ‘deselectByValue(String value)’

Code Snippet:

				
					public static void main(String[] args) throws InterruptedException {
WebDriver driver= new ChromeDriver();
driver.get("https://grotechminds.com/multiple-select/");
WebElement dropdown= driver.findElement(By.id("automobiles"));
Select sel=new Select(dropdown);
sel.selectByIndex(0);
sel.selectByValue("sedan");
sel.selectByVisibleText("Hatchback");
sel.selectByIndex(3);
sel.deselectByValue("motorcycle");
sel.deselectByValue("hatchback");
}

				
			

After deselection:

t
  1. To deselect a selection based on the String value (textual representation) of the option in the list, we use a method of the ‘Select’ class called ‘deselectByVisibleText(String text)’

Code Snippet:

				
					public static void main(String[] args) throws InterruptedException {
WebDriver driver= new ChromeDriver();
driver.get("https://grotechminds.com/multiple-select/");
WebElement dropdown= driver.findElement(By.id("automobiles"));
Select sel=new Select(dropdown);
sel.selectByIndex(0);
sel.selectByValue("sedan");
sel.selectByVisibleText("Hatchback");
sel.selectByIndex(3);
Thread.sleep(3000);
sel.deselectByVisibleText("Motorcycle");
sel.deselectByVisibleText("Sedan");
}

				
			

After deselection:

unnamed (1)
  1. To deselect all the options in the list we use this method of the ‘Select’ class called ‘deselectAll()’

Code Snippet:

				
					public static void main(String[] args) throws InterruptedException {
WebDriver driver= new ChromeDriver();
driver.get("https://grotechminds.com/multiple-select/");
WebElement dropdown= driver.findElement(By.id("automobiles"));
Select sel=new Select(dropdown);
sel.selectByIndex(0);
sel.selectByValue("sedan");
sel.selectByVisibleText("Hatchback");
sel.selectByIndex(3);
sel.deselectAll();
}

				
			

After deselection:

unnamed (2)
Conclusion

We are likely to face a situation where we must perform selections and deselections on a multi-selection drop-down box. Remember to practise, stay updated with the latest trends in Automation Software Testing Course,and maintain a positive attitude throughout your interview process.  Now, we know how to tackle those situations and come out successfully.

Also read:

Upskill Yourself
Consult Us