How to Select random value from a DropDownList using Selenium or UFT

Requirement:- We need to select random value from a given dropdownlist, to test the functionality how our AUT would behave under varying selection of options.
Application Link:–

DropDownlist for random selection Selenium
DropDownlist + HTML code

1.We would take the count of existing options in the weblist/dropdownlist .
2.Take a random value between 0(starting index of the dropdownlist, we can avoid in our case as we have –Select– at 0 index) and maximum value -1(total available options in the list)
3.Select the value based on the random value

Same logic would work whether we are using Selenium or UFT as our testing tool.

1.How to take count of items:-
a.We can use Findelements method to return a list of items and further we would use getSize method of list to fetch the count value.

//Creating the List object
List <WebElement> weblist = driver.findElements(By.xpath(".//*[@id='drpdwnTopics']/option"));
//Taking the count of items
int iCnt = weblist.size();

b.We can use the Select Class and implement its getOptions method to get the count.

Select objSel = new Select(driver.findElement(By.xpath(".//*[@id='drpdwnTopics']")));
List <WebElement> weblist = objSel.getOptions();
//Taking the count of items
int iCnt = weblist.size();

2.How to generate random number, we would implement “Random” class and use its nextInt method to output the random number between the given range.
Note:- nextInt, method would generate number from 0 to upperlimit-1, example nextInt(10), would generate numbers between 0 and 9.


import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.*;

public class RandomSelectionDrpDwnList {
WebDriver driver;
public void launch()
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20000, TimeUnit.MILLISECONDS);

public void selectRandom()
//Object of the Dropdownlist
WebElement drpDwnList = driver.findElement(By.xpath(".//*[@id='drpdwnTopics']"));
//Using FindElements to create a List object
//List <WebElement> weblist = driver.findElements(By.xpath(".//*[@id='drpdwnTopics']/option"));
//Using Select Class to fetch the count
Select objSel = new Select(drpDwnList);
List <WebElement> weblist = objSel.getOptions();
//Taking the count of items
int iCnt = weblist.size();
//Using Random class to generate random values
Random num = new Random();
int iSelect = num.nextInt(iCnt);
//Selecting value from DropDownList
//Selected Value

public void close()
//Closing the browser


1.For taking count of items, we would use GetRoProperty in UFT and pass “all items” to fetch the count 
2. Random number is a Utility object , which generates random number between the given range


'Random number is a Utility object , which generates random number between the given range
Dim obj
Set objList = Browser("creationtime:=0").Page("creationtime:=0").webList("html id:=drpdwnTopics")
fnSelectRandom objList

'Function Name:- fnSelectRandom
'Function Description:- Function to Select Random Value from DropDown
'Input Parameters:- objList(WebList)
'Output Parameters:- None
Sub fnSelectRandom(objList)
Dim iCnt,iRandom,sSelValue
'Getting the item count of dropdown
iCnt = objList.GetROProperty("items count")
'Taking the Random range between 0 and items of Dropdown
iRandom = RandomNumber(0,iCnt-1)
'Selecting the value
objList.Select "#" & iRandom
'Taking the Selected Random Value
Print "Selected Value = "&objList.Object.value
End Sub

More Examples
Learning Java 
Learn UFT


8 thoughts on “How to Select random value from a DropDownList using Selenium or UFT

  1. In UFT:- set the RandomNumber function range from 1 rather than 0.
    Simple approach is check that after randomization selected value is not 'select'
    like if(drpdownoption == 'select') than call random function again.

  2. Getting java.lang.NullPointerException
    at RandomSelectionDrpDwnList.selectRandom(
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(
    at org.testng.internal.Invoker.invokeMethod(
    at org.testng.internal.Invoker.invokeTestMethod(
    at org.testng.internal.Invoker.invokeTestMethods(
    at org.testng.internal.TestMethodWorker.invokeTestMethods(
    at org.testng.TestRunner.privateRun(
    at org.testng.SuiteRunner.runTest(
    at org.testng.SuiteRunner.runSequentially(
    at org.testng.SuiteRunner.privateRun(
    at org.testng.SuiteRunnerWorker.runSuite(
    at org.testng.TestNG.runSuitesSequentially(
    at org.testng.TestNG.runSuitesLocally(
    at org.testng.TestNG.runSuites(
    at org.testng.remote.RemoteTestNG.initAndRun(
    at org.testng.remote.RemoteTestNG.main(

  3. I get why you want to select random options. You want to automate the testing a human conduct. But, as much as in might seem so from a computer's perspective, human's don't typically select random options from a select box. Real users select the option that applied to them. The point of automated testing is not to select for random cases, but to select for ALL cases. But again, kudos, for solving the puzzle.

Leave a Reply

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