What would be a walkaround for JavaScript alerts that are generated in a page's onload() using Selenium?

感情迁移 提交于 2019-12-07 12:41:54

问题


I am automating a form page using Selenium RC (C#). After I click 'Submit' button, I get an alert 'Records Edited Successfully!'. The title of this alert box is 'The page at http://www.******.com says:'.

But Selenium doesn't see this alert. And I can't work around it.

Here is what I've tried:

selenium.Click("ctl00_Content_ctl00_btnSubmit");
selenium.WaitForPageToLoad("30000");

Result: I get the following error: "Selenium.SeleniumException : Timed out after 30000ms"

Then I tried:

selenium.Click("ctl00_Content_ctl00_btnSubmit");
selenium.OpenWindow("", "The page at The page at http://www.******.com says:");
selenium.Close();
selenium.WaitForPageToLoad("30000");

Result: Three windows are opened (site, alert and extra window). Nothing gets closed. I get the following error: "Selenium.SeleniumException : Timed out after 30000ms"

Then I tried:

selenium.Click("ctl00_Content_ctl00_btnSubmit");
selenium.SelectWindow("The page at The page at http://www.******.com says:");
selenium.Close();
selenium.WaitForPageToLoad("30000");

Result: I get the following error: "Could not find window with title 'The page at http://www.******.com says:'"

Any suggestions? Please help to overcome this obstacle.


回答1:


Apparently the easiest way to do this is to use script to redefine the alert() function to something that doesn't popup a dialog.

((JavascriptExecutor) fDriver).executeScript(
  "window.alert = function(msg) { return true; }"
); 



回答2:


Finally I've found a workaround:

    selenium.Click("ctl00_Content_ctl00_btnSubmit");                        
    Thread.Sleep(5000);
    selenium.KeyDownNative("32");
    selenium.KeyUpNative("32");

Wish you all the best, everyone!




回答3:


This post seems to suggest a solution:

  • http://groups.google.com/group/selenium-users/browse_thread/thread/ba4d2f05f721350b

Also, check this post:

  • http://groups.google.com/group/selenium-users/browse_thread/thread/6df962e73da5ac22


来源:https://stackoverflow.com/questions/4210658/what-would-be-a-walkaround-for-javascript-alerts-that-are-generated-in-a-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!