I'm using selenium web driver in a java swings application to open a web page in single tab but when i'm closing the browser manually i'm not getting any event of it and its not working for me.Is there a way to get the browser closing event in selenium ?
Asked
Active
Viewed 3,654 times
2
-
What you mean by closing event , please give example. – Helping Hands Jun 25 '15 at 11:20
-
possible duplicate of this http://stackoverflow.com/questions/29491334/determine-when-the-browser-is-closed-when-using-selenium-webdriver – Sarfaraz Khan Jun 25 '15 at 11:54
-
closing events means when i'm closing the web browser manually i m not able to track that event in my swings application using selenium. – Ruchika Jun 29 '15 at 10:14
1 Answers
-1
I solved it by capturing the exception,i was getting when i was closing the browser manually and then try to close it again at the close of my application. From that catch block only i'm reopening the browser by instantiating the web driver again.
catch (Exception _e) {
log.error(_e);
System.out.println("Browser already closed");
//launching the web browser again
String path="";
if(browserName.contains("chrome")){
try {
path = System.getProperty("user.dir")+"\\BrowserExeFiles\\chromedriver.exe";
OpenURL.driver = new ChromeDriver();
} catch (Exception e) {
e.printStackTrace();
}
}
else if(browserName.contains("ie")){
try{
path=System.getProperty("user.dir")+"\\BrowserExeFiles/ie.exe";
OpenURL.driver = new InternetExplorerDriver();
} catch (Exception e) {
e.printStackTrace();
}
}
else if(browserName.contains("firefox")){
String firefox_Path = getFirfoxPath();
File pathToBinary = new File(firefox_Path);
FirefoxBinary ffBinary = new FirefoxBinary(pathToBinary);
FirefoxProfile firefoxProfile = new FirefoxProfile();
driver = new FirefoxDriver(ffBinary,firefoxProfile);
}
}
driver.get(urlString);
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
}

Procrastinator
- 2,526
- 30
- 27
- 36

Ruchika
- 29
- 1
- 4
-
This isn't catching THE window close exception, it's catching every single exception thrown, which is generally a really bad practice. ElementNotFoundException? Fire up a new browser. TimeoutException? Fire up a new browser. You get the idea... – JeffC Feb 28 '23 at 17:50