Webdriver select list from dropdown

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Webdriver select list from dropdown

Ashwin
This post was updated on .
Hi,

I have a selenium RC code below which selects list of drop down values and stores in array

String [] abc = selenium.getSelectOptions("c");
                for(int i=1; i<abc.length; i++)
                        log.info(i+" : "+abc[i]);

how to do the same with webdriver

can anyone help me.

Thanks,
Ashwin
               
Reply | Threaded
Open this post in threaded view
|

Re: Webdriver select list from dropdown

softwaretestingforum
Administrator
Hi Ashwin,

I would suggest to you use List instead of String to store the values. You could use WebDriver to achieve this as following -

               // List to hold drop down values
                List<String>dropDownValues = new ArrayList<String>();
               
                // List to hold drop down elements in drop down
                List<WebElement> dropDownElement = new Select(driver.findElement(By
                                .cssSelector("drop down locator"))).getAllSelectedOptions();
               
                // Store drop down values
                for(WebElement element: dropDownElement) {
                        dropDownValues.add(element.getText());
                       
                }
               
                // Print drop down values
                for(String value: dropDownValues) {
                        System.out.println(value);
                }


Let us know if you have any doubt.
~ seleniumtests.com