如何使用WebDriver随机选择按钮?(How do I select a button randomly with WebDriver?)

编程入门 行业动态 更新时间:2024-10-20 07:54:26
如何使用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();

更多推荐

本文发布于:2023-08-02 18:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1379633.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   按钮   WebDriver   randomly   select

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!