Wednesday, June 26, 2024

Handling Multi Select dropdown Selenium Java

 

Multi Select dropdown:

 

Handling multi-select dropdowns using Selenium WebDriver involves selecting multiple options simultaneously, which can be achieved by simulating keyboard actions or using the Select class methods provided by Selenium.

 

Single Selection dropdown - we can select only "one/single" value at a time

Multi Select dropdown (or) Multi Select List box - we can select "multiple" values at a time 

(Press ctrl + click  Benz, Audi)

                                                           

------------

ex: MultiSelectDropdownBasics1

package WebelementsBasics;

 

import java.util.List;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.support.ui.Select;

 

public class MultiSelectDropdownBasic1 {

 

            public static void main(String[] args) throws InterruptedException {

                        System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

                        WebDriver  driver = new ChromeDriver();

 

                        Thread.sleep(3000);

                        // open url -file:///C:/brahma/Practise/qtp%20practise/web%20apps/SampleWebpage.html

                        driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/SampleWebpage.html");

 

                        //select  'Maruthi' in Multi Select dropdown?

                        WebElement multiCarsDropdownEle = driver.findElement(By.name("multicars"));

                       

                        Select sel = new Select(multiCarsDropdownEle);

                        // select Audi 

                        sel.selectByVisibleText("Audi");

 

                        //  Select  'Kia' in Multi Sleect dropdown

                        sel.selectByVisibleText("Kia");

 

                        //HW  invalid  Volksswagen

 

                        // org.openqa.selenium.NoSuchElementException: Cannot locate option with text: Volksswagen

 

 

                        // HW 2.select Maruthi, Audi values by using SelectByValue()

 

                        // HW 3. select 1st, 2nd value by using  Select By Index ()

 

                        // HW  get all dropdown values count

 

                        // HW get all vals from Multi Select drop down?

 

 

                        // get all selected values from Multi select dropdown ?

                        //                                                             selectedValsFromDropdown=Maruthi

                        //                                                                     selectedValsFromDropdown=Benz

                        //                                                                     selectedValsFromDropdown=Audi

                       

                        //                                                getptions()

                        List<WebElement> getAllSelectedOptions =  sel.getAllSelectedOptions();

                                   

                         //  String, Webele, List<Webele>

                        //  <option>  </option>

                       

                        // count of selected values

                        int cnt = getAllSelectedOptions.size();

 

                        System.out.println("selectedValsCnt=" + cnt); // 4

 

                        // display all selected values

                        for(int i=0;i<=cnt-1;i++)

                        {

                                    String selectedVal =getAllSelectedOptions.get(i).getText();

                                    System.out.println("selectedValues from Multi Select dropdown ="+selectedVal );

                        }

                                   

 

                        // HW 2.get all selected values using "for each loop"

 

                        // Hw 3. get all selected values using "iterator()

                       

                        // HW 4.get all selected values using "listIterator()

 

                        // Check Dropdown is multiple Selection or single selection

//                     <select multiple="" name="multicars">

 

                        boolean isMultiple = sel.isMultiple();

                        //                           true

                        //  isMultiple  = true

                        //  can be used to check given dropdown is "multiple" selection or not

                        // if it is "Multi select" dropdown , this method returns true else false

                        System.out.println("isMultiple=");// true

                        // Accenture -wrtten Test

 

            }

 

}

 

 

// HW check cars dropdown   is Multi Select dropdown or single select dropdown ?

                        // Accenture : FAQ   Written Test

 

DeSelect :

SelectByVisibleText()

selectByValue()

SelectByIndex()

In Selenium, the Select class provides methods to interact with multi-select dropdowns. Here are the methods you can use to deselect options:

·  deselectByVisibleText(String text): Deselects an option by the visible text of the option.

·  deselectByValue(String value): Deselects an option by the value attribute of the option.

·  deselectByIndex(int index): Deselects an option by its index in the dropdown list (indexing starts from 0).

·  deselectAll(): Deselects all options if the dropdown is a multi-select.These methods are only applicable to multi-select dropdowns.

 

 

press ctrl +  click on selected value  -- deselect vaue

-------------------------

package WebelementsBasics;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.support.ui.Select;

 

public class MultiSelectDropdownDeselect {

 

            public static void main(String[] args) throws InterruptedException {

                        System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

                        WebDriver  driver = new ChromeDriver();

 

                        Thread.sleep(3000);

                        // open url -file:///C:/brahma/Practise/qtp%20practise/web%20apps/SampleWebpage.html

                        driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/SampleWebpage.html");

 

                        //select  'Maruthi' in Multi Select dropdown?

                        WebElement multicarsDropdownEle =  driver.findElement(By.name("multicars"));

                        Select  sel  = new Select(multicarsDropdownEle);

 

                        sel.selectByVisibleText("Maruthi");  

                        Thread.sleep(4000);

                        // select Audi 

                        sel.selectByVisibleText("Audi");

 

                        Thread.sleep(4000);

                        // Deselect 'Maruthi' value in dropdown

                        sel.deselectByVisibleText("Maruthi");

 

                        Thread.sleep(4000);

                        // Deselect 'Audi' value in dropdown

                        sel.deselectByValue("audival");

 

                        // HW deSelectByValue()

 

                        // HW DeselectByIndex ()                  

 

                        // deselect all values in Multi Select drodpown                   

                        sel.deselectAll();

 

            }

 

}

 

                       

 

-------------------------

HW WAP to select all values in Multi Select dropdown?

 but We don’t how many values in Multi select dropdown

Hint :   SelectbyIndex () use for loop also

// 0 -first  1 -  2nd val

 

 

HW Write a separate method  to check given value is exist in cars dropdown

            // if value is exist, method must return true else false

 

 

boolean isValueExistInDropdown(Strin val)

{

  //......   code

 

}

 

 isValueExistInDropdown("Honda"); // true

 isValueExistInDropdown("Audi"); // true

 isValueExistInDropdown("Honda21312"); // false

 

 

 

Revision:

Select vals in dropdown:

  class :   -->

  methods :    3  -->  

 if given text is not there,  noSuchelement Exception 

  get cnt :    List<webelement>  alloptions = sel.getOptions();

                                    size()

 get all vals  form dropdown

 

MultiSelect :  

        select vals :   

            deselect vals :

            deselect all : 

            check drodown is MultiSlect or not : 

           

            get only slected dropdown :   

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...