使用Java在Selenium WebDriver(selenium 2)中进行警报处理 [英] Alert handling in Selenium WebDriver (selenium 2) with Java
本文介绍了使用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?
推荐答案
这个从我这里使用显式等待 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)中进行警报处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文