Java org.openqa.selenium.UnhandledAlertException:意外警报打开
我正在使用Chrome驱动程序并尝试测试网页 正常情况下运行良好,但有时会出现异常:Java org.openqa.selenium.UnhandledAlertException:意外警报打开,java,javascript,exception,selenium,selenium-webdriver,Java,Javascript,Exception,Selenium,Selenium Webdriver,我正在使用Chrome驱动程序并尝试测试网页 正常情况下运行良好,但有时会出现异常: org.openqa.selenium.UnhandledAlertException: unexpected alert open (Session info: chrome=38.0.2125.111) (Driver info: chromedriver=2.9.248315,platform=Windows NT 6.1 x86) (WARNING: The server did not pro
org.openqa.selenium.UnhandledAlertException: unexpected alert open
(Session info: chrome=38.0.2125.111)
(Driver info: chromedriver=2.9.248315,platform=Windows NT 6.1 x86) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 16 milliseconds: null
Build info: version: '2.42.2', revision: '6a6995d', time: '2014-06-03 17:42:30'
System info: host: 'Casper-PC', ip: '10.0.0.4', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_25'
Driver info: org.openqa.selenium.chrome.ChromeDriver
然后我尝试处理警报:
Alert alt = driver.switchTo().alert();
alt.accept();
但这次我收到了:
org.openqa.selenium.NoAlertPresentException
我正在附上警报的屏幕截图:
我现在不知道该怎么办。问题是我并不总是收到这个异常。当它发生时,测试失败。您是否在try/catch块中切换到警报?您可能还需要添加等待超时,以查看警报是否在特定延迟后显示
try {
// Add a wait timeout before this statement to make
// sure you are not checking for the alert too soon.
Alert alt = driver.switchTo().alert();
alt.accept();
} catch(NoAlertPresentException noe) {
// No alert found on page, proceed with test.
}
您可以尝试以下代码段:
public void acceptAlertIfAvailable(long timeout)
{
long waitForAlert= System.currentTimeMillis() + timeout;
boolean boolFound = false;
do
{
try
{
Alert alert = this.driver.switchTo().alert();
if (alert != null)
{
alert.accept();
boolFound = true;
}
}
catch (NoAlertPresentException ex) {}
} while ((System.currentTimeMillis() < waitForAlert) && (!boolFound));
}
public void acceptablertifavailable(长超时)
{
long waitForAlert=System.currentTimeMillis()+超时;
布尔布尔函数=false;
做
{
尝试
{
Alert Alert=this.driver.switchTo().Alert();
如果(警报!=null)
{
alert.accept();
boolFound=true;
}
}
catch(NoAlertPresentException ex){}
}而((System.currentTimeMillis()
您可以使用Selenium WebDriver中的等待
功能来等待警报,并在警报可用时接受警报
在C#-
它在Java中的等价物-
public static void HandleAlert(WebDriver driver, WebDriverWait wait) {
if (wait == null) {
wait = new WebDriverWait(driver, 5);
}
try {
Alert alert = wait.Until(new ExpectedCondition<Alert>{
return new ExpectedCondition<Alert>() {
@Override
public Alert apply(WebDriver driver) {
try {
return driver.switchTo().alert();
} catch (NoAlertPresentException e) {
return null;
}
}
}
});
alert.Accept();
} catch (WebDriverTimeoutException) { /* Ignore */ }
}
publicstaticvoidhandleAlert(WebDriver驱动程序,WebDriverWait等待){
if(wait==null){
等待=新的WebDriverWait(驱动程序,5);
}
试一试{
警报警报=等待。直到(新的预期条件{
返回新的ExpectedCondition(){
@凌驾
公共警报应用(WebDriver){
试一试{
返回驱动程序.switchTo().alert();
}捕获(NoAlertPresentException e){
返回null;
}
}
}
});
alert.Accept();
}catch(WebDriverTimeoutException){/*忽略*/}
}
它将等待5秒钟,直到出现警报。如果预期警报不可用,您可以捕获异常并处理它。单击事件后,添加下面的代码以进行处理
try{
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
catch (org.openqa.selenium.UnhandledAlertException e) {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText().trim();
System.out.println("Alert data: "+ alertText);
alert.dismiss();}
。。。做其他事情
driver.close() 我也有这个问题。这是由于驾驶员在遇到警报时的默认行为造成的。默认行为设置为“接受”,因此警报将自动关闭,而switchTo().alert()无法找到它 解决方案是修改驱动程序的默认行为(“忽略”),使其不会关闭警报:
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);
d = new FirefoxDriver(dc);
然后你可以处理它:
try {
click(myButton);
} catch (UnhandledAlertException f) {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
System.out.println("Alert data: " + alertText);
alert.accept();
} catch (NoAlertPresentException e) {
e.printStackTrace();
}
}
以下是我的工作
private void acceptSecurityAlert() {
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(10, TimeUnit.SECONDS)
.pollingEvery(3, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
Alert alert = wait.until(new Function<WebDriver, Alert>() {
public Alert apply(WebDriver driver) {
try {
return driver.switchTo().alert();
} catch(NoAlertPresentException e) {
return null;
}
}
});
alert.accept();
}
private void acceptSecurityAlert(){
Wait Wait=new FluentWait(驱动程序)。带超时(10,TimeUnit.s)
.轮询间隔(3,时间单位。秒)
.忽略(NoSuchElementException.class);
Alert Alert=等待.until(新函数(){
公共警报应用(WebDriver){
试一试{
返回驱动程序.switchTo().alert();
}捕获(NoAlertPresentException e){
返回null;
}
}
});
alert.accept();
}
您可以使用
UnexpectedAlertBehaviour.ACCEPT
或UnexpectedAlertBehaviour.discouse
我遇到了同样的问题,我做了以下更改
try {
click(myButton);
} catch (UnhandledAlertException f) {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
System.out.println("Alert data: " + alertText);
alert.accept();
} catch (NoAlertPresentException e) {
e.printStackTrace();
}
}
下面的代码将有助于处理selenium中的意外警报
try{
} catch (Exception e) {
if(e.toString().contains("org.openqa.selenium.UnhandledAlertException"))
{
Alert alert = getDriver().switchTo().alert();
alert.accept();
}
}
当遇到弹出的未受伤害的警报框时抛出。您需要将代码设置为正常运行,除非找到警报框场景。这就克服了你的问题
try {
System.out.println("Opening page: {}");
driver.get({Add URL});
System.out.println("Wait a bit for the page to render");
TimeUnit.SECONDS.sleep(5);
System.out.println("Taking Screenshot");
File outputFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String imageDetails = "C:\\images";
File screenShot = new File(imageDetails).getAbsoluteFile();
FileUtils.copyFile(outputFile, screenShot);
System.out.println("Screenshot saved: {}" + imageDetails);
} catch (UnhandledAlertException ex) {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
System.out.println("ERROR: (ALERT BOX DETECTED) - ALERT MSG : " + alertText);
alert.accept();
File outputFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String imageDetails = "C:\\Users";
File screenShot = new File(imageDetails).getAbsoluteFile();
FileUtils.copyFile(outputFile, screenShot);
System.out.println("Screenshot saved: {}" + imageDetails);
driver.close();
} catch (NoAlertPresentException e) {
e.printStackTrace();
}
}
我尝试了下面的代码,它非常适合我(Chrome)
这可能是根本原因吗?对fxdriver.modals.clearFlag的绕过调用\u。。。cf.ChromeDriver上的问题有FirefoxDriver解决方案,这很奇怪。这是否意味着chrome无法克服这种默认行为。如果驱动程序的默认行为是接受所有警报,那么为什么它首先抛出异常?它不应该自动接受警报并继续吗?也许是为了确保开发人员知道警报正在打开?这意味着它没有在设置的情况下单击警报。为什么会发生这种情况?这种警报间歇性出现的原因是什么?
try{
} catch (Exception e) {
if(e.toString().contains("org.openqa.selenium.UnhandledAlertException"))
{
Alert alert = getDriver().switchTo().alert();
alert.accept();
}
}
UnhandledAlertException
try {
System.out.println("Opening page: {}");
driver.get({Add URL});
System.out.println("Wait a bit for the page to render");
TimeUnit.SECONDS.sleep(5);
System.out.println("Taking Screenshot");
File outputFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String imageDetails = "C:\\images";
File screenShot = new File(imageDetails).getAbsoluteFile();
FileUtils.copyFile(outputFile, screenShot);
System.out.println("Screenshot saved: {}" + imageDetails);
} catch (UnhandledAlertException ex) {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
System.out.println("ERROR: (ALERT BOX DETECTED) - ALERT MSG : " + alertText);
alert.accept();
File outputFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String imageDetails = "C:\\Users";
File screenShot = new File(imageDetails).getAbsoluteFile();
FileUtils.copyFile(outputFile, screenShot);
System.out.println("Screenshot saved: {}" + imageDetails);
driver.close();
} catch (NoAlertPresentException e) {
e.printStackTrace();
}
}
try{
System.out.println("Waiting for Alert");
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.alertIsPresent()).dismiss();
System.out.println("Alert Displayed");
}
catch (Exception e){
System.out.println("Alert not Displayed");
}