简体   繁体   中英

Multi select drop down

I am writing code for multi select using selenium(java) where i need the following task to be performed :

  1. select multiple options in drop down
  2. to click on button first selected which will print the first selected option from drop down.
  3. to click on button get all selected which will print the all selected options in order .

I have this code which returns me undefined as a result for 2nd task.

public class MultipleSlectList {

    public static WebDriver driver ;

    @BeforeTest
    public void startbrowser () throws Exception {
        System.out.println("launching browser");
        System.setProperty("webdriver.gecko.driver", "H:\\Selenium3\\geckodriver-v0.19.1-win32\\geckodriver.exe");
        driver = new FirefoxDriver();
        driver.get("http://www.seleniumeasy.com/test/basic-select-dropdown-demo.html");

    }

    @Test

    public void selectlist () throws Exception {

        WebElement ele1 = driver.findElement(By.id("multi-select"));
        Select se= new Select(ele1);
        se.selectByValue("New Jersey");
        Thread.sleep(2000);
        se.selectByValue("Texas");
        Thread.sleep(2000);
        se.selectByValue("Florida");
        Thread.sleep(2000);
        //Thread.sleep(10000);
        WebElement btn1= driver.findElement(By.id("printMe"));
        btn1.click(); // it is supposed to return New Jersy 
        WebElement firstOption = se.getFirstSelectedOption();
        System.out.println("The First selected option is::" +firstOption.getText());
        List <WebElement> oSize = se.getAllSelectedOptions();

        int iListSize = oSize.size();
        // Setting up the loop to print all the options
        for (int i = 0; i < iListSize; i++)
        {
            // Storing the value of the option  
            String sValue = oSize.get(i).getText();
            // Printing the stored value
            System.out.println(sValue);
        }
    }
}

Please help me to proceed further.

I have tried with jquery as well but no luck .The result is same as "undefined" in both case .

Thanks !

Steps to select all dropdown options:

  • Find the SELECT WebElement by webdriver.
  • Create Select class which is used to operate dropdown list.
  • Get all options list in the dropdown list.
  • Loop the options list, get each option value and use
  • Select.selectByValue(optionValue) to select it.
  • Then all the dropdown option has been selected.

Below article has the code example to implement above scenario.

http://www.dev2qa.com/select-dropdown-list-selenium-webdriver/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM