Deselecting the Selected Options Using Selenium
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.
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”.
- 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)’
- 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)’
- 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:
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.
- 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:
- 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:
- 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:
- 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:
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:
Consult Us