开发者

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

开发者 https://www.devze.com 2023-01-25 21:04 出处:网络
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

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.


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; }"
); 


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消