0

I am testing price comparisons web application and I would like to randomly select one of the "Apply Now" buttons. I had tried: (and other similar scenario example from stack)

But every time it select 1st Apply now button. I would like to select different button every time. HTML:

<div class="applyNowButtonContainer">
<a class="join-now" data-productid="******" data-excessid="******" data-href="/XXXXX/ApplyNow_prepare.action?rank=1&hash=onclick="Within.joinNow(this);" href="javascript:void(0)">Apply Now</a>
</div>
oberlies
  • 11,503
  • 4
  • 63
  • 110

2 Answers2

1

Here you go:

// get all links by xpath
List<WebElement> links = driver.findElements(By.xpath("//*[contains(text(), 'Apply Now')]));

// select a random one
Random gen = new Random();
WebElement link = links.get(gen.nextInt(list.size()));

link.click();
Fabricator
  • 12,722
  • 2
  • 27
  • 40
0

Query all buttons by type or class and use random index on the array of buttons

Here is how you can get random index within the range: Generating unique random numbers (integers) between 0 and 'x'

Community
  • 1
  • 1
Bobz
  • 2,394
  • 1
  • 19
  • 20