简体   繁体   English

如何使用WebDriver随机选择一个按钮?

[英]How do I select a button randomly with WebDriver?

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. 但是每次选择“立即申请1st”按钮。 I would like to select different button every time. 我想每次选择不同的按钮。 HTML: 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>

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

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' 这是获取范围内随机索引的方法: 生成介于0和'x'之间的唯一随机数(整数)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM