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()) {
driver.switchTo().window(windowName);
}
}
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”
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
I am using executeautomation demo application for this scenario –
steps:
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);
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?
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){
driver.switchTo().alert().accept();
}
//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){
driver.switchTo().alert().sendKeys(message);
}
//Click cancel button in alerts, confirmation and prompt windows
public static void cancelPopup(WebDriver driver){
driver.switchTo().alert().dismiss();
}
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
window.showModalDialog
which shows a html document called ResponsePopUp.aspx:
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
Thanks,
Rakesh
Hi Karthik,
I am using executeautomation demo application for this scenario –
steps:
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?
Thanks,
Guru
I hope before you take screenshot, you need to switch to that window.
Thanks,
Karthik KK
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 = itererator.next();
driver.switchTo().window(mainWinID);
}
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){
driver.switchTo().alert().accept();
}
//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){
driver.switchTo().alert().sendKeys(message);
}
//Click cancel button in alerts, confirmation and prompt windows
public static void cancelPopup(WebDriver driver){
driver.switchTo().alert().dismiss();
}
Awesome !!!
I will include these snippets in the page.
Thank you Janesh for your contribution.
Thanks
Karthik KK
How to insert a value in the table in the popup menu?
I also want to insert value in popup menu. In case if you got the answer kindly convey it.
Hi, i want to read the data from pop up window and paste that data in the excel file. how can i do this?
What sort of popup window is that ? Custom Javascript window or browser-based JS Window ?
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
window.showModalDialog
which shows a html document called ResponsePopUp.aspx:
Hi,sir
I have found the system popup but how is handle the system popup by using the selenim.