How can I randomly click a button on a web page

Hi helpful people in my PC.

I have done a search but I havent quite seen anything to cover my dilemma.

I have a website that I want Katalon to randomly click on a button on the page. At the moment, I am filtering it and it clicks the first one on the page, but I would like it to click on others. My page is similar to this one (Its not Tui that I work for, I just borrowed their page)

and all of the “buttons” are like this (hidden company info)

so how would I get Katalon to randomly click one of the buttons? the products change in test so im a little bit stuck on what I could do.

This is my test'Search filter drop down/filter))‘Search filter drop down/filter2’))‘Search filter drop down/filter3’))

WebUI.delay(2)‘Search filter drop down/Both/FilterButton’))

WebUI.delay(2)‘Search filter drop down/hotel’))‘Search filter drop down/Both/DepartureDate’))‘Search filter drop down/HighestPrices’))

not_run:‘Search filter drop down/Both/Page2Results’))

WebUI.delay(2)‘Search filter drop down/Both/Cruise button (U)’))


String Availiability1 = ‘document.querySelector(“.pricebutton”).click()’

WebUI.executeJavaScript(Availiability1, null )

thanking you in advance for any input you can give.

1 Like

You might be able to if you can collect those “buttons” into a list and then use the size of the list within your random function. Below is my code for a random selection of one of our personnel to pick within a drop-down list.

import java.util.Random
import org.openqa.selenium.WebDriver as WebDriver
import org.openqa.selenium.WebElement as WebElement
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory

def driver = DriverFactory.getWebDriver();

Random rand = new Random();'myPage/input_GeneralSalesAgent'))
WebUI.setText(findTestObject('myPage/input_GeneralSalesAgent'), "Smith")

'To locate rows, it will capture all the rows available in the drop-down'
List<WebElement> rows_table = driver.findElements(By.xpath('//div[@class="selector-options-item"]'));
WebUI.comment("Number of items: " + rows_table.size())
upperLimit = rows_table.size();
// here we select one of the random agents
1 Like

Thank you :heart:. I will try that and see how I get on but it now gives me something to go on. Thanks again for taking the time to help me, its is much appreciated :blush: