简体   繁体   中英

Get browser closing event in selenium web browser

我在Java Swing应用程序中使用硒网络驱动程序在单个选项卡中打开网页,但是当我手动关闭浏览器时,我没有收到任何事件并且它对我不起作用。获取硒中的浏览器关闭事件?

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM