How do i can click random item of dropdown list?

Hey Guys,
suppose I have following HTML

   <li>Test 1</li>
   <li>Test 2</li>
   <li>Test 3</li>

i want to click random item (test 1 or test 2 or test 3)
please give any solution for that.

can I store those all “li” tag as a TestObject into array list?

Assuming that you already have a Test Object that identifies all of the <li> elements:

List<WebElement> elements = WebUiCommonHelper.findWebElements(myTestObject, 30);
WebElement randomElement = elements.get(new Random().nextInt(elements.size()));;
1 Like

Thank your support so much @Brandon_Hein :+1:
It’s work with me.

Im getting error, sorry. May I know what should i do with im having issue with them.

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
file:/C:/Users/asabitsana/Katalon%20Studio/Fastrax%20Core/Scripts/scroll/Script1634187882419.groovy: 48: unable to resolve class WebElement
@ line 48, column 6.
List elements = WebUiCommonHelper.findWebElements(findTestObject(‘Object Repository/test/Page_FMIS/span_ASSET CATEGORY_k-select’), 30)

file:/C:/Users/asabitsana/Katalon%20Studio/Fastrax%20Core/Scripts/scroll/Script1634187882419.groovy: 50: unable to resolve class WebElement
@ line 50, column 12.
WebElement randomElement = elements.get(new Random().nextInt(elements.size()))

2 errors

at com.kms.katalon.core.main.ScriptEngine.getScript(
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(
at com.kms.katalon.core.main.TestCaseExecutor.runScript(
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(
at com.kms.katalon.core.main.TestCaseExecutor.execute(
at com.kms.katalon.core.main.TestCaseMain.runTestCase(
at com.kms.katalon.core.main.TestCaseMain.runTestCase(
at com.kms.katalon.core.main.TestCaseMain$runTestCase$ Source)

You just need to import some classes. Press ctrl + shift + o on your keyboard.

You need to write an import statement for WebElement class in the header part of your test case script. That is:

import org.openqa.selenium.WebElement;

The javadoc of this class is published here:

How can I find the fully qualified class name?

Either of the followings:

  1. Try searching pages for WebElement, or
  2. Press Ctrl + Shiift + o, as @Brandon_Hein suggested. KS knows what WebElement is so that it will resolve the fully qualified class name for you.