如何使用WebDriver随机选择按钮?(How do I select a button randomly with WebDriver?)
我正在测试价格比较网络应用程序,我想随机选择一个“立即应用”按钮。 我试过:(和堆栈中的其他类似场景示例)
但每次选择1st Apply now按钮。 我想每次都选择不同的按钮。 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>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>最满意答案
干得好:
// 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();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();更多推荐
发布评论