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 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)

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:‘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()));

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:

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.

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)