How do i can click random item of dropdown list?

Hey Guys,
suppose I have following HTML

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

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()));
randomElement.click();
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 randomElement.click? im having issue with them.

Reason:
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(ScriptEngine.java:199)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:430)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:421)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:400)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:392)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:273)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:142)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:133)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1634542753000.run(TempTestCase1634542753000.groovy:25)

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.