本文介绍了使用 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) 中处理警报
发布评论