How To Select Random Value From Any Drop down In Selenium Web Driver

Hello Guys,

When you are automating any functionality, you may need to select value from drop down. What should be selected value, generally you want it from user. There might be many scenarios where you just need to select any value from drop down.

For example: Consider User registration where you need to select country , gender and date of birth from drop down and you do not required any special values provided by external sources. You just need to select any values from drop down to proceed further. In this case we should select any value from drop down.

Consider another scenario of running your test scripts in multiple environment where drop down has different values based on environment.  You need to prepare separate data sheet as per environment which will not be easy to maintain.

There might be big problem, if value passed from external source is not available in drop down causes test script failure.

So the point is, if you do not require to select any specific value from drop down, you must select any random value. It will reduce the maintaining of test data in external files as well as unexpected test script failure.

How to select any random value from drop down:

  1. First identity what type of drop down it is.
  2. Get all options available in drop down.
  3. Generate a random number within 0 and number of options. You can set min and max as per your requirements.
  4. Select option available at random number position.

Example code:

Output: Run above program, you will see how different values are selected in drop down.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.

12 thoughts on “How To Select Random Value From Any Drop down In Selenium Web Driver

  1. Thanks for your script however I am trying to run it and it not working. The program informed me that: line at 38 39 error, with content: “no such element: Unable to locate element: {“method”:”xpath”,”selector”:”//div[@class=’menu transition visible’]”}”

    1. May be locator is changed. This post shows the way of doing that. As it is third party application, locator may change.

  2. hi
    i have a doubt
    can u please explain how i have to retrieve min and max values from an integer type dropdown provided if the dropdown is in div’s

    i am able to get the count of options but i cannot get the value of each option e.,g 1,2,3 like this

  3. Hi Amod,
    Your below code:
    WebElement dropdown = driver.findElement(By.xpath(“//div[text()=’Select Friend’]”));
    points to a different drop-down &
    your below code:
    .visibilityOf(driver.findElement(By.xpath(“//div[@class=’menu transition visible’]”))));
    points out to a different element,so after running your code it throws NosuchElementException

    1. Hi Vaibhav,
      I am using some url which might be changed as it is not my personal. Major idea of this program to give an idea to select random value in a dropdown. Thanks.

  4. Hi Mukesh,
    I am little bit confusion why you wrote below line, already in selenium drop down related command “.getOptions()” is there

    //your code:
    // Getting list of options
    List itemsInDropdown = driver
    findElements(By.xpath(“//div[@class=’menu transition visible’]//div[@class=’item’]”));

    My Suggestion:
    List listofOptions= driver.findElement(By.xpath(“//div[text()=’Select Friend’]”)).getOptions();

    Could you please let me know there is any difference

Leave a Reply

Your email address will not be published. Required fields are marked *