javascript selenium webdriver 模式对话框 java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14549770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
selenium webdriver modal dialog java
提问by John
I am testing my form and when I don't type needed data I get javascript alert in my web app that tells the user to enter missing data. I can't handle this with selenium because when I partially fill form and try to submit I get exception
我正在测试我的表单,当我没有输入所需的数据时,我会在我的网络应用程序中收到 javascript 警报,告诉用户输入丢失的数据。我无法用 selenium 处理这个问题,因为当我部分填写表格并尝试提交时,我得到了异常
org.openqa.selenium.UnhandledAlertException: Modal dialog present
If I catch exception the alert in webdriver is not shown. Is that any solution to solve this issue?I would like to be able to submit form and catch the alert. I am using Linux Mint,Firefox 18 and selenium 2.28.0 with java Best regards UPDATE I have following in my code
如果我捕获异常,则不会显示 webdriver 中的警报。这是解决这个问题的任何解决方案吗?我希望能够提交表单并收到警报。我正在使用 Linux Mint、Firefox 18 和 selenium 2.28.0 和 java 最好的问候 UPDATE 我在我的代码中遵循
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)
However if I click manual on submit the test work as expected. Thanks in advance
但是,如果我按预期单击手动提交测试工作。提前致谢
回答by lAH2iV
you should handle the alert as soon as the action is done and there shouldn't be any other action before handling the alert.
您应该在操作完成后立即处理警报,并且在处理警报之前不应该有任何其他操作。
for instance your code should be
例如你的代码应该是
try{
submit.click();
if (alertPresent())
getCurrentAlert();
if(urlChanged()){
return new AdminHome(driver);
}
}
This will check alert and then accept the alert. The interaction of webdriver is more similar to the action we interact with manually with browser. So when the click on submit is done we will be able to see alert and no actions can be done until accept or reject it.
这将检查警报,然后接受警报。webdriver的交互更类似于我们手动与浏览器交互的动作。因此,当单击提交完成后,我们将能够看到警报,并且在接受或拒绝之前无法执行任何操作。
Vishal
维沙尔
回答by Code Enthusiastic
It is because driver accepts the alert itself when the UnhandledAlertException is thrown. How can you submit the form if you have filled it partially?
这是因为驱动程序在抛出 UnhandledAlertException 时接受警报本身。如果您填写了部分表格,您如何提交表格?
If it is even possible, just catch that exception, and in catch block write the line which clicks on the submit button.
如果有可能,只需捕获该异常,并在 catch 块中写入单击提交按钮的行。
回答by Taras_Rusynyak
Use Robot class (Press enter) to close modal dialog box
使用机器人类(按回车键)关闭模态对话框
try {
(new Robot()).keyPress(java.awt.event.KeyEvent.VK_ENTER);
(new Robot()).keyRelease(java.awt.event.KeyEvent.VK_ENTER);
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}