How to Randomly choose a Dropdown Value

HTML is using span and I can’t use some of the codes suggested on how to randomly choose a value on the dropdown.

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)

Do you have the import statement for WebElement in your list?

import org.openqa.selenium.WebElement as WebElement

thank you for your reply @grylion54. Error is gone but unfortunately it’s still not selecting any values. Below are the codes used:

WebUI.click(findTestObject(‘Object Repository/test/Page_FMIS/span_ASSET’))

List elements = WebUiCommonHelper.findWebElements(findTestObject(‘Object >Repository/test/Page_FMIS/span_ASSET CATEGORY_k-select’), 30)

WebElement randomElement = elements.get(new Random().nextInt(elements.size()))

randomElement.click();

Is there any error message that you are given?

Can you determine if elements is getting a value by adding a comment to the code, like below?

List<WebElement> elements = WebUiCommonHelper.findWebElements(findTestObject('Object Repository/test/Page_FMIS/span_ASSET CATEGORY_k-select'), 30)

WebUI.comment("Found ${elements.size()} elements!")

WebElement randomElement = elements.get(new Random().nextInt(elements.size()))

I’m getting this in the Console tab:

2021-11-15 08:25:36.184 DEBUG testcase.scroll - 12: elements = findWebElements(findTestObject(“Object Repository/test/Page_FMIS/span_ASSET CATEGORY_k-select”), 30)
2021-11-15 08:25:36.659 DEBUG testcase.scroll - 13: comment(Found $elements.size() elements!)
2021-11-15 08:25:36.697 INFO c.k.k.c.keyword.builtin.CommentKeyword - Found 1 elements!
2021-11-15 08:25:36.698 DEBUG testcase.scroll - 14: randomElement = elements.get(Random().nextInt(elements.size()))
2021-11-15 08:25:36.699 DEBUG testcase.scroll - 15: randomElement.click()

The WebUI.comment() statement displays in the Log Viewer tab (you will see it display as comment) like other WebUI statements. If you want to review the value in the Console tab, then you will have to change to:

List<WebElement> elements = WebUiCommonHelper.findWebElements(findTestObject('Object Repository/test/Page_FMIS/span_ASSET CATEGORY_k-select'), 30) 

println("Found ${elements.size()} elements!") 

WebElement randomElement = elements.get(new Random().nextInt(elements.size()))

I notice also that you are trying to click on a span element. Perhaps you should review that as well. I would have expected an input element to click on instead or, since your heading is about “a dropdown value”, I might even suspect a select element, such as the one below the span in your image above.

Yeah, that’s my concern, elements are in span. can you help me which code should I use?

@shopaholiclet You will have to give a little bit more information, such as if we are still discussing the “dropdown value” or have you moved onto something else, on what you are trying to do and what the HTML code is or is it still the same as in your first post?

still the same dropdown value. Like how can I choose the 1st or 2nd option/values. I’m sorry I’m quite new to automation and I have very limited knowledge in coding.

There are generally 3 WebUI methods you can use to select a dropdown value (of a select tag). They are: WebUI.selectOptionByLabel(), WebUI.selectOptionByValue() and WebUI.selectOptionByIndex().

The selectOptionByLabel() method uses the text the select tag displays.
e.g.

WebUI.selectOptionByLabel(findTestObject('myPage/select_PersonChoice'), "Grylion54", false)

The selectOptionByValue() method uses the value attribute the select tag has. This you will have to review the HTML to find.
e.g. (rather than choosing to use the person’s name, I choose the person’s reference “value”)

WebUI.selectOptionByValue(findTestObject('myPage/select_PersonChoice'), "1087", false)

The selectOptionByIndex() method uses the position of the choice, starting from 0, within the list of options the select tag has.
e.g. (in this case, I just take the first name in the list–zero is the blank or no name)

WebUI.selectOptionByIndex(findTestObject('myPage/select_PersonChoice'), 1)

These only work on a select tag.

And also

Also, to confirm your other options within the select tag, you can review others in the list:

WebUI.verifyOptionPresentByLabel(findTestObject('myPage/select_PersonChoice'), "Major12", false, 10)
WebUI.verifyOptionPresentByLabel(findTestObject('myPage/select_PersonChoice'), "JJ Cooper", false, 10)

And to confirm the option you selected actually was selected, you can add:

WebUI.verifyOptionSelectedByLabel(findTestObject('myPage/select_PersonChoice'), "Grylion54", false, 10)