Selenium WebDriver how to close browser popup

前端 未结 9 989
悲&欢浪女
悲&欢浪女 2020-11-27 18:29

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 a

相关标签:
9条回答
  • 2020-11-27 18:46

    Put one of these before the click event:

    selenium.chooseCancelOnNextConfirmation()
    
    selenium.chooseOkOnNextConfirmation()
    
    0 讨论(0)
  • 2020-11-27 18:48

    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.

    0 讨论(0)
  • 2020-11-27 18:49
    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
    
    0 讨论(0)
  • 2020-11-27 18:52

    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.

    0 讨论(0)
  • 2020-11-27 19:01

    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();
    }
    
    0 讨论(0)
  • 2020-11-27 19:02

    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){};}
    
    0 讨论(0)
提交回复
热议问题