Site Loader
Auckland, New Zealand
In this post we will discuss handing and working in popup window using Selenium WebDriver. There are situations, where we might need to work with a popup windows like entering data within a text control, selecting a dropdown control etc, but by default, selenium Webdriver does not know if there is a popup window, until you specify to it explicitly. Hence, to specify explicitly about the popup window, first we need to get the handle of window opened using getWindowHandle() method of WebDriver. Using this handle, we can pass it as a parameter for the SwitchTo().Window() method of WebDriver to start working in popup window. Here is the complete video of the above discussion. Here is the complete code for the above discussion
public static void SwitchWindow(WebDriver driver) {
	for (String windowName : driver.getWindowHandles()) {
Thanks for watching the video and reading the post !!! Please leave your comments and let me know if there is anything need to improved in this post. Thanks, Karthik KK

Post Author: Karthik kk

12 Replies to “Handing Popup window in Selenium”

  1. Hi Karthik,

    In my webpage, i am getting an unexpected pop-up, if we know the pop-up will come , we can accept or switch to that.

    Kindly let me know how to handle unexpected pop-ups


  2. Hi Karthik,

    I am using executeautomation demo application for this scenario –
    1. Click on “Generate” button on Login screen – Popup comes on screen [here I am considering this as unexpected popup]
    2. Try to enter some value in “Initial” field.[Popup window will prevent this action and throws exception event]

    To handle unexpected exception(popup), I am using below code
    public static void PlaybackIssues()
    firingDriver = new EventFiringWebDriver(driver);

    firingDriver.ExceptionThrown += firingDriver_TakeScreenshotOnException;
    Driver = firingDriver;

    public static void firingDriver_TakeScreenshotOnException(object sender, WebDriverExceptionEventArgs e)
    Console.WriteLine(“exception occurred”);

    string timestamp = DateTime.Now.ToString(“yyyy-MM-dd-hhmm-ss”);
    ((ITakesScreenshot)Driver).GetScreenshot().SaveAsFile(ScreenShotFile + “Exception-” + timestamp + “.png”, System.Drawing.Imaging.ImageFormat.Png);
    While execution, when I get exception I am able to fire event but automatically popup window clicks on “Cancel” button and then “OK” button and screenshot is not taken.

    Could you please explain how to do user customized actions on such popup?


    1. I hope before you take screenshot, you need to switch to that window.

      Karthik KK

  3. Following code (also) could be used for getting the window IDs and switch between the main and popup windows.

    public void switchToMainWindow(WebDriver driver){
    Set windowId = driver.getWindowHandles();
    Iterator itererator = windowId.iterator();
    String mainWinID =;

  4. Following could be useful for managing alerts, confirmation and prompt windows

    //Click ok button in alerts, confirmation and prompt windows
    public static void closePopup(WebDriver driver){

    //Get message in alerts, confirmation and prompt windows
    public static String getPopupMessage(WebDriver driver){
    return driver.switchTo().alert().getText();

    //Type given text in a prompt window
    public static void typeInPrompt(WebDriver driver, String message){

    //Click cancel button in alerts, confirmation and prompt windows
    public static void cancelPopup(WebDriver driver){

    1. Awesome !!!

      I will include these snippets in the page.

      Thank you Janesh for your contribution.

      Karthik KK

    1. I also want to insert value in popup menu. In case if you got the answer kindly convey it.

  5. HI Karthik
    I have a confirmation popup that I think is a javascript prompt in my web application that I cannot identify using typical browser developer tools toinspect and identify the web elements. Hence I cannnot get the Xpath, Css, ID etc to tell Selenium to select the OK button. I have tried swqitchTo for Window handles, Alerts, iFrames and Prompts.

    The web application code uses a javascript call to


    which shows a html document called ResponsePopUp.aspx:



  6. Hi,sir
    I have found the system popup but how is handle the system popup by using the selenim.

Leave a Reply

Your email address will not be published. Required fields are marked *