开发者

Selenium WebDriver how to close browser popup

I am writing tests for a web App using selenium webDriver and came across a scenario where when I try to close the browser I get a popup saying "Are you sure? The page is asking y开发者_C百科ou to confirm that you want to leave - data entered will be lost." with 2 buttons: Leave Page and Stay on Page

How do I click on those buttons?


 ( ( JavascriptExecutor ) _driver )
            .executeScript( "window.onbeforeunload = function(e){};" );

solved the issue for me


IAlert alert = driver.SwitchTo().Alert();

alert.Accept(); //for two buttons, choose the affirmative one
// or
alert.Dismiss(); //to cancel the affirmative decision, i.e., pop up will be dismissed and no action will take place


You can interact with alerts and the like using the Alert API. Switching to the alert and confirm it would look like this (in Java):

Alert alert = driver.switchTo().alert();
alert.accept();

This is also explained in the Selenium FAQ.


def close_all_popups(driver):
    driver.window_handles
    for h in driver.window_handles[1:]:
        driver.switch_to_window(h)
        driver.close()
    driver.switch_to_window(driver.window_handles[0])


You might try using keyboard events. So once the window pops up:

Tab onto the "Ok" button.

driver.Keyboard.PressKey(Keys.Tab);

You'll have to play around to see how many tab presses are required.

Then hit space.

driver.Keyboard.PressKey(Keys.Space);

Yeah it's sort of a hack, but WebDriver is still pretty immature.

EDIT: This will work for "real" popups, but as another answerer said, maybe not for weird in-page things. Basically, if you can tab to the close button manually, this method should work.


Not sure what is the structure of the popup that you have used. Here are a few that may work for you if you have used either of as to popup.

If its an alert. you can try:

driver.SwitchTo().Alert()

If its a window that pops up then:

driver.SwitchTo().Window(<windowname>)

For iFrame:

driver.SwitchTo().Frame("iFrmLinks")

Once you get through either of the three then you can access all its internal elements.

Regards Tahir


I've got the same problem when i have the form of fields and "done editing" submit button, because when Selenium IDE auto-click the javascript function, that responsible to disable confirmation window (leave page or still on it), it does not take "mouseup" mouse event that mean window.confirm still works and auto-pass test was fails. My solution is override javascript function window.onbeforeunload in this case (no need to ask if we know that we do when we record test in Selenium IDE). You can run override script in the Selnium IDE before click on "Save" (or something like this) button through selenium.runScript - it should simple disable the confirmation window.

    Command: runScript
    Target: {window.onbeforeunload=function(e){};}


You need to handle the unexpected alerts using try catch blocks. Put your code in try block and catch the 'UnhandledAlertException'

Example:

try{
     .....
     .....
     code here
}catch(UnhandledAlertException e ){
    driver.switchto().alert().dismiss();
}


Put one of these before the click event:

selenium.chooseCancelOnNextConfirmation()

selenium.chooseOkOnNextConfirmation()
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜