How to close the new browser pop up window in Selenium WebDriver??

18,979 views
Skip to first unread message

sirus tula

unread,
Sep 21, 2012, 1:00:28 PM9/21/12
to seleniu...@googlegroups.com
Hello Selenium Experts,
 
I am having trouble closing the new browser pop up which appears after clicking the button. I am able to open a new window and switch to the main window but not able to close the main window.
 
When i say driver.close() after swithcing to new window, it closes the main window and fails the test.
 
Can anyone experience this problem?
 
If yes, can you please help me find the solution.
 
Any help would be appreciated.
 
 
Below is the webdriver code that I have.
 
 
     public void Test1()
        {
            Methods Common = new Methods(driver); //calls the common class
           
            string originalHandle= driver.CurrentWindowHandle;   //gets the current window handle
            driver.FindElement(By.LinkText("Developer")).Click();
 
            if (Common.IsElementPresent(By.CssSelector("a[class='app12']")))
            {
                Common.waitforElement(By.CssSelector("a[class='app12']"));
                driver.FindElement(By.CssSelector("a[class='app12']")).Click();
            }
 
            else if (Common.IsElementPresent(By.CssSelector("div[class='bicon']")))
            {
                driver.FindElement(By.CssSelector("div[class='bicon']")).Click();  //opens a new browser window after clicking the button
            }
            
            driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromMilliseconds(500));
            driver.Close();  //closes the main window instead of popup and fails.
            driver.SwitchTo().Window(originalHandle);
            Common.waitforElement(By.CssSelector("button[class='success']"));
           
        }
 
 
Thanks,
 
Sirus

Ashok Tulachan

unread,
Sep 21, 2012, 1:08:46 PM9/21/12
to seleniu...@googlegroups.com

Correction: I mean to say i'm not able to close the new browser pop up window.
 
Thanks,
 
Sirus.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Regards,
 
Ashok Tulachan
 

Neeraj Sharma

unread,
Sep 21, 2012, 1:09:55 PM9/21/12
to seleniu...@googlegroups.com
Yes, this is because you are closing the driver before switching to the new window, see your code,

sirus tula

unread,
Sep 21, 2012, 1:13:11 PM9/21/12
to seleniu...@googlegroups.com
 
Thank you for your prompt response, Niraj.
 
After the new pop up window is displayed, I also use the following code but the driver is still picking up the main window instead of new window.

string newWindow = seeker.CurrentWindowHandle;

driver.SwitchTo().Window(newWindow);  // it still selects the main window

driver.Close();

drivr.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromMilliseconds(500));

driver.SwitchTo().Window(originalHandle);

Thanks
 
Sirus

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/zBdHmdPD_IcJ.

sirus tula

unread,
Sep 21, 2012, 1:13:46 PM9/21/12
to seleniu...@googlegroups.com
Correction, seeker=driver.
 
Thanks :)

Neeraj Sharma

unread,
Sep 21, 2012, 1:21:23 PM9/21/12
to seleniu...@googlegroups.com
read this thread and might be you are able to find your solution, i have already posted a working demo code there for dealing with multiple windows.

sirus tula

unread,
Sep 21, 2012, 1:27:08 PM9/21/12
to seleniu...@googlegroups.com
Thanks Niraj, NOT sure how it would help on this issue though.
 
Seems like this is webdriver defect although I found out the work around solution.
 
String newWindow= driver.CurrentWindowHandle; //still refers to the main window not sure why.
 
However, here's the work around solution that i found.
 
driver.SwitchTo().Window(driver.WindowHandles.ToList().Last()); // switches to the new window
driver.close(); // Now closes the new window
 
 
Thanks,
 
Sirus
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/WP7EC02AW54J.

Neeraj Sharma

unread,
Sep 21, 2012, 1:34:07 PM9/21/12
to seleniu...@googlegroups.com
your problem is same as first of all you are not able to switch to the new window. and if this problem is solved then you can proceed further so first try to solve this problem. Can you please give me any URL related to your application so that i can show you the same code for that to deal with the windows.

Jim Evans

unread,
Sep 21, 2012, 1:35:51 PM9/21/12
to seleniu...@googlegroups.com
WebDriver does not automatically switch to new windows when opened. You have to do that yourself, so it's not surprising (nor is it a bug) that the .CurrentWindowHandle property would still refer to the main window, even though you've clicked on the link to open a new window. Notice also that the list of window handles returned by the .WindowHandles property is not guaranteed to be in any order. That means you cannot rely on the last handle in the list being the last window opened.

In the .NET support classes (WebDriver.Support.dll) there is a PopupWindowFinder class that provides an example of how to properly implement this behavior. You're welcome to use the class as-is, but it's intended to be an example, so if it doesn't meet your needs 100%, you'll need to implement your own version.

Ashok Tulachan

unread,
Sep 21, 2012, 1:46:38 PM9/21/12
to seleniu...@googlegroups.com

Thank u Jim again. Not sure how many times we have exchanged emails :).

It's very hard to find help on webdriver c# problems since many people use java and there are only handful of webdriver c# experts like u :).

Thanks again :).

To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/JFOeQsKM8WgJ.

sat

unread,
Feb 28, 2014, 11:44:55 AM2/28/14
to seleniu...@googlegroups.com
Hi all,

What is the best way to close the browsers after each run (Especially Firefox ).

I am getting below error (If I forget to close my FF Browser manually ,before run the each  test )

Any idea from your side?

To avoid the issue ,I have to  close the the browser manually ,before run the each  test .

Thanks,
SAT

==============================
========================================================================================================

org.testng.TestNGException:
Cannot instantiate class UserManagement.UserTest
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:38)
    at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:387)
    at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:299)
    at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:110)
    at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:186)
    at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:120)
    at org.testng.TestRunner.initMethods(TestRunner.java:409)
    at org.testng.TestRunner.init(TestRunner.java:235)
    at org.testng.TestRunner.init(TestRunner.java:205)
    at org.testng.TestRunner.<init>(TestRunner.java:160)
    at org.testng.remote.RemoteTestNG$1.newTestRunner(RemoteTestNG.java:141)
    at org.testng.remote.RemoteTestNG$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG.java:271)
    at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:561)
    at org.testng.SuiteRunner.init(SuiteRunner.java:157)
    at org.testng.SuiteRunner.<init>(SuiteRunner.java:111)
    at org.testng.TestNG.createSuiteRunner(TestNG.java:1299)
    at org.testng.TestNG.createSuiteRunners(TestNG.java:1286)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
    ... 21 more
Caused by: org.openqa.selenium.UnsupportedCommandException: unknown command: hub/session
Command duration or timeout: 980 milliseconds
Build info: version: '2.38.0', revision: 'bd32d4e', time: '2013-12-05 16:15:38'
System info: host: 'waessve', ip: '10.194.11.166', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_45'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:216)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:111)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:197)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:190)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:186)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:99)
    at UserManagement.UserTest.<init>(UserTest.java:26)
    ... 26 more

Shawn McCarthy

unread,
Feb 28, 2014, 12:56:01 PM2/28/14
to seleniu...@googlegroups.com
Put something like the following in an AfterMethod ? That way it will run even if the test fails:

if(driver != null) {
    driver.quit();

Vikas Gahlaut

unread,
Mar 10, 2014, 4:55:20 AM3/10/14
to seleniu...@googlegroups.com
driver.close() will close current window and driver.quit will close all window
Reply all
Reply to author
Forward
0 new messages