使用 Java 在 Selenium WebDriver (selenium 2) 中处理警报

编程入门 行业动态 更新时间:2024-10-09 03:31:28
本文介绍了使用 Java 在 Selenium WebDriver (selenium 2) 中处理警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想检测是否弹出警报.目前我正在使用以下代码:

I want to detect whether an alert is popped up or not. Currently I am using the following code:

try { Alert alert = webDriver.switchTo().alert(); // check if alert exists // TODO find better way alert.getText(); // alert handling log().info("Alert detected: {}" + alert.getText()); alert.accept(); } catch (Exception e) { }

问题是如果网页的当前状态没有警报,它会等待特定的时间,直到超时,然后抛出异常,因此性能非常糟糕.

The problem is that if there is no alert on the current state of the web page, it waits for a specific amount of time until the timeout is reached, then throws an exception and therefore the performance is really bad.

有没有更好的方法,也许我可以使用警报事件处理程序来处理动态发生的警报?

Is there a better way, maybe an alert event handler which I can use for dynamically occurring alerts?

推荐答案

这对我来说使用 Explicit Wait from here WebDriver:高级用法

This is what worked for me using Explicit Wait from here WebDriver: Advanced Usage

public void checkAlert() { try { WebDriverWait wait = new WebDriverWait(driver, 2); wait.until(ExpectedConditions.alertIsPresent()); Alert alert = driver.switchTo().alert(); alert.accept(); } catch (Exception e) { //exception handling } }

更多推荐

使用 Java 在 Selenium WebDriver (selenium 2) 中处理警报

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

发布评论

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

>www.elefans.com

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