isMultiple() method is used to identify if you can select multiple options from a select element.
deselectAll() method is used to deselect all selections from a select element.
Now you can easily select or deselect any specific option from the select box or drop down as described in my earlier POST 1, POST 2 and POST 3. All these three posts will describe different alternative ways of selecting options from the list box or drop-down. Now let me take you one step ahead to describe you the use of deselectAll() and isMultiple() methods in selenium webdriver.
Use of deselectAll() Method in selenium
deselectAll() method is useful to remove the selection from all selected options of the select box. It will work with multiple select boxes when you need to remove all selections. The syntax for the deselectAll() method is as follows.
Select listbox = new Select(driver.findElement(By.xpath("//select[@name='FromLB']")));
listbox.deselectAll();
Use of isMultiple() Method in selenium
Purpose of isMultiple() method is used to verify if the targeted select box is a multiple selection box or not. That means we can select multiple options from that select box or not. It will return a boolean (true or false) value. We can use it with if condition before working with the select box. Syntax is as bellow.
Example of isMultiple() Method in selenium
Select listbox = new Select(driver.findElement(By.xpath("//select[@name='FromLB']")));
boolean value = listbox.isMultiple();
System.out.print(value);
Example of deselectAll() method in selenium
The example will show you the use of isMultiple() and deselectAll() methods very clearly in Selenium webdriver.@Test
public void test () throws InterruptedException
{
Select listbox = new Select(driver.findElement(By.xpath("//select[@name='FromLB']")));
//To verify that specific select box supports multiple select or not.
if(listbox.isMultiple())
{
System.out.print("Multiple selections is supported");
listbox.selectByVisibleText("USA");
listbox.selectByValue("Russia");
listbox.selectByIndex(5);
Thread.sleep(3000);
//To deselect all selected options.
listbox.deselectAll();
Thread.sleep(2000);
}
else
{
System.out.print("Not supported multiple selections");
}
}
In the above example, if condition will checks the listbox is multiple select box or not? Used listbox.deselectAll(); to remove all selected options.
You can VIEW MORE SELENIUM WEBDRIVER BASIC COMMANDS.
No comments:
Post a Comment