how to click drop down options one after one by web driver

3,646 views
Skip to first unread message

prakash suryawanshi

unread,
Aug 29, 2014, 2:42:17 PM8/29/14
to webd...@googlegroups.com
Hi,

I am trying to work on multiple drop down options on "http://www.amazon.in/".

Here i want to select options in 'search' drop down one after one.Means if i selected 'Books' option and click then
again it should go and click on another option like 'Music' .

Please refer my below code:

public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new ChromeDriver();
driver.get("http://www.amazon.in/");
driver.findElement(By.cssSelector("#searchDropdownBox")).click();
driver.findElement(By.cssSelector("#searchDropdownBox > option:nth-child(2)")).click();
String valueToSelect= null;
WebElement select = driver.findElement(By.cssSelector("#searchDropdownBox"));
Select dropDown = new Select(select);           
String selected = dropDown.getFirstSelectedOption().getText();
    List<WebElement> Options = dropDown.getOptions();
for(WebElement option:Options){
if(option.getText().equals(valueToSelect)){
if(selected.equals("books"))
option.isSelected();
}
if(selected.equals("music"))
option.isSelected();
}}
amazon.in.jpg

Mrunal Gosar

unread,
Aug 30, 2014, 10:07:40 AM8/30/14
to webd...@googlegroups.com
Hi Prakash
Please try below code its working and let me know.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;


public class Prakash
{
 
@FindBy(id="searchDropdownBox")
 
WebElement dropdown;
 
 
public Prakash(WebDriver driver)
 
{
 
PageFactory.initElements(driver, this);

 
}
 
public static void main(String[] args)
 
{

 
WebDriver driver=new FirefoxDriver();
 driver
.manage().window().maximize();
 driver
.get("http://www.amazon.in/");
 
Prakash p=new Prakash(driver);
 
Select options=new Select(p.dropdown);
 
System.out.println("First selected options :- "+options.getFirstSelectedOption().getText());
 
for (WebElement option : options.getOptions())
 
{
 
System.out.println("Option :- "+option.getText());
 options
.selectByVisibleText(option.getText());
 
}
 driver
.close();
 
}
}

Regards
Mrunal

prakash suryawanshi

unread,
Aug 30, 2014, 11:11:48 AM8/30/14
to webd...@googlegroups.com

Hi Mrunal,

Thanks for ur reply.

I tried ur program but my basic requirement is to select and click one option(it will search by that option) and then select another one option
and click(it will search by another option). I have to click options by selecting and do not requires only text values of options.



regards,
prakash

Mrunal Gosar

unread,
Aug 30, 2014, 11:19:55 AM8/30/14
to webd...@googlegroups.com
Hi Prakash
options.selectByVisibleText("your options text");

The above code does the same which you have requested you just have to run it in a loop with your desired values. You have to pass the text of the option which you want to select. Let me know if i have not understood your question correctly?

Regards
Mrunal


prakash suryawanshi

unread,
Aug 30, 2014, 12:27:37 PM8/30/14
to webd...@googlegroups.com
Hi Mrunal,

Yes, actually my requirement is different than u hv mentioned.I need to click on one option and immediately select and click
on another option.

In above codes for options i dont know how to apply it for multiple options to select and click.
I only need to select and click on options one by one and do not print values of options. 

for ex: options like sort by price/date/popularity in drop down..


regards,
prakash

Mrunal Gosar

unread,
Aug 30, 2014, 1:32:28 PM8/30/14
to webd...@googlegroups.com
 
 By select you mean that the drop down window should open and then you will click on the option you want to select, then again you want to open the drop down window and again select some option. If thats what you want, then you can do one thing, instead of using Select, try to identify the element using some locator and mimic the actions which you want to perform instead of using methods from Select class. Let me know if you need further help on this.

prakash suryawanshi

unread,
Aug 31, 2014, 10:29:51 AM8/31/14
to webd...@googlegroups.com
Hi Mrunal,

i tried to identify element and it is  located but needs code that will work for multiple options click
in drop down.



regards,
prakash  

darrell

unread,
Sep 1, 2014, 10:14:02 AM9/1/14
to webd...@googlegroups.com
If I understand you correctly you want to go to http://www.amazon.in/, go to the SELECT to the left of the search box and select multiple options. If you try to do this manually you will find you cannot. If you cannot do it manually then you cannot automate it.

prakash suryawanshi

unread,
Sep 1, 2014, 1:55:39 PM9/1/14
to webd...@googlegroups.com
Hi Darrell,

I am not selecting multiple options at one time instead of that first I selecting suppose 'Books' option and once it get selected
then I am trying to select and click on another option.


regards,
prakash 

darrell

unread,
Sep 2, 2014, 8:06:17 AM9/2/14
to webd...@googlegroups.com
Still not clear what your test case is. Are you doing:

- load page
- select an option
- select a different option

Or is you test case:

- load page
- select first option
- select second option
- select third option
- continue to select options until the last option is reached

or is the test case:

- load page
- select option
- search for something
- select another option
- search for something

These are all very different test cases. I always determine what I am trying to test before I determine how to test it. Selenium (and all other automation tools) is a tool. Tools should not drive the work; the work should drive which tool to use. I'd recommend you post your test case (forget about HOW you are going to automate it and just give us WHAT you want to test).

prakash suryawanshi

unread,
Sep 2, 2014, 10:40:02 AM9/2/14
to webd...@googlegroups.com
Hi Darrell,

Below is my test case:

- load page
- select first option
- select second option
- select third option
- continue to select options until the last option is reached



regards,
prakash

darrell

unread,
Sep 3, 2014, 8:59:04 AM9/3/14
to webd...@googlegroups.com
What are you attempting to do? This is not a valid test case. Additionally, you seem to be making this more difficult than it needs to be:

        // load web page
        driver.get("http://www.amazon.in/");
        // find the SELECT element and use it to construct a org.openqa.selenium.support.ui.Select
        Select s = new Select(driver.findElement(By.cssSelector("#nav-searchbar select")));
        // get is list of all the OPTION tags under the SELECT
        List<WebElement> options = s.getOptions();
        // for each OPTION tag
        for(WebElement option : options) {
            // select the OPTION
            option.click();
            // just to show it is working I print out the visible text of each OPTION
            System.out.printf("%s\n", option.getText());

prakash suryawanshi

unread,
Sep 3, 2014, 10:36:01 AM9/3/14
to webd...@googlegroups.com

Hi Darrell,

i want to select and click one drop down option and search then again select and click another option and search by it.

I am not expecting to print all options from drop down.

Here i do select and click option and search by it.By mistake I mentioned that select one option and then select another option.

How can I do this for multiple options here. 

Sorry for inconvenience as i forgot to mention 'search' step here.



regards,
prakash

darrell

unread,
Sep 4, 2014, 8:46:05 AM9/4/14
to webd...@googlegroups.com
Ah, that is very different. So you test case becomes:

- Select the first option
- Search for keyword
- Select the second option
- Search for keyword
- ...
- Select the last option
- Search for keyword

Where ... is repeat for all the options on the select. As a programmer I look at this list of requirements and think, this could be converted into a loop. So I'm going to rework the requirements to be:

- Get a list of the options available
- For each option:
    - Select the 'next' option
    - Search for keyword

If I write this as:

        String keyword = "Selenium";
        driver.get("http://www.amazon.in/");
        Select s = new Select(driver.findElement(By.cssSelector("#nav-searchbar select")));
        List<WebElement> options = s.getOptions();
        for(WebElement option : options) {
            option.click();
            driver.findElement(By.cssSelector("#twotabsearchtextbox")).sendKeys(keyword);
            driver.findElement(By.cssSelector("[value='Go']")).click();
        }

It looks like I'm done but when you run this you will find that selecting the second option throws an error. The moment you search for the first option it reloads the page. The reload of the page makes all List of WebElement go stale. So when you try to access the second option the loop fails. So you cannot loop on the WebElement. You need to figure out how to select the different options using Java data types and not WebElement data types. Essentially, you need to locate the options inside the loop, save the locator then use a second loop to loop over the locator strings:

        String keyword = "Selenium";
        driver.get("http://www.amazon.in/");
        Select s = new Select(driver.findElement(By.cssSelector("#nav-searchbar select")));
        List<WebElement> options = s.getOptions();
        List<String> optionsLocators = new ArrayList<>();

        // get the locators into a List of String
        for(WebElement option : options) {
            optionsLocators.add(String.format("#searchDropdownBox>option[value='%s']", option.getAttribute("value")));
        }

        // loop over the List of String because the WebElement will go stale
        driver.findElement(By.cssSelector("#twotabsearchtextbox")).sendKeys(keyword);
        for(String optionLocator : optionsLocators) {
            WebElement option = driver.findElement(By.cssSelector(optionLocator));
            option.click();
            driver.findElement(By.cssSelector("[value='Go']")).click();
        }

SuperKevy

unread,
Sep 4, 2014, 9:34:07 AM9/4/14
to webd...@googlegroups.com
Your asking Darrell to do your work.

Manually what steps do you do?
1. Select the list item
2. Click the Search button.   It may be as simple as browser.button(:value,'Go').click

prakash suryawanshi

unread,
Sep 5, 2014, 7:51:20 AM9/5/14
to webd...@googlegroups.com
Hi Darrell,

Thanks for ur help.
Its working fine.


regards,
prakash
Reply all
Reply to author
Forward
0 new messages