java+selenium自动化遇到confirm弹窗,出现如下异常:
Exception in thread “main” org.openqa.selenium.NoAlertPresentException: no alert open
解决方案见下面代码段
//操作js的confirm弹窗,bool控制是否点击确定,true为点击确定,false为点击取消 public static void OperaterJSOfConfirm(WebDriver driver,String str,Boolean bool) throws InterruptedException, AWTException{ WebElement button = driver.findElement(By.xpath(str)); button.click(); Thread.sleep(2000); /*Alert lert = driver.switchTo().alert(); Thread.sleep(1000);//睡眠2″,否则可能会提示没有打开的lert错误 if(bool){ lert.accept(); }else{ lert.dismiss(); }*/ //解决上面代码经常出现的“no alert open”异常情况 Robot robot = new Robot();//在基本屏幕坐标系中构造一个 Robot 对象 robot.keyPress(KeyEvent.VK_SPACE);//按下给定的键 robot.keyRelease(KeyEvent.VK_SPACE);//释放该键 }