我正在测试我的表单,当我没有键入所需的数据时,我在我的网络应用程序中收到 javascript 警报,告诉用户输入缺失的数据。我无法用 selenium 处理这个问题,因为当我部分填写表格并尝试提交时出现异常
org.openqa.selenium.UnhandledAlertException: Modal dialog present
如果我捕获异常,则不会显示 webdriver 中的警报。这是解决这个问题的任何解决方案吗?我希望能够提交表格并收到警报。我在 Java 中使用 Linux Mint、Firefox 18 和 selenium 2.28.0 此致 更新 我的代码中有以下内容
somePage.fillName(sth); //only 1 of 2 required field are filled
somgePage.submit(); //here js alert is shown right after clicking submit
somePage.getCurrentAlert();
//here are code parts
public Alert getCurrentAlert(){
return driver.switchTo().alert();
}
public AdminHome submit(){
saveUrl();
WebElement submit = driver.findElement(By.id("add_quiz_submit_button"));
try{
submit.click();
if(urlChanged()){
return new AdminHome(driver);
}
}
catch(Exception e){
e.printStackTrace();// exception 1
return null;
}
return null;
}
//Exception 1
org.openqa.selenium.UnhandledAlertException: Modal dialog present
//The test fails because of:
org.openqa.selenium.NoAlertPresentException: No alert is present (WARNING: The server did not provide any stacktrace information)
但是,如果我按预期点击手动提交测试工作。提前致谢
最佳答案
您应该在操作完成后立即处理警报,并且在处理警报之前不应有任何其他操作。
例如你的代码应该是
try{
submit.click();
if (alertPresent())
getCurrentAlert();
if(urlChanged()){
return new AdminHome(driver);
}
}
这将检查警报,然后接受警报。 webdriver的交互更类似于我们手动与浏览器交互的action。因此,当点击提交完成后,我们将能够看到警报,并且在接受或拒绝之前无法执行任何操作。
维沙尔
关于java - selenium webdriver 模式对话框 java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14549770/