在selenium Web浏览器中获取浏览器关闭事件 [英] Get browser closing event in selenium web browser

查看:1837
本文介绍了在selenium Web浏览器中获取浏览器关闭事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在java swings应用程序中使用selenium web驱动程序在单个选项卡中打开一个网页但是当我手动关闭浏览器时我没有得到它的任何事件而且它对我不起作用。我在那里在selenium中获取浏览器关闭事件的方法吗?

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 ?

推荐答案

我通过捕获异常解决了这个问题,当我到达时我得到了手动关闭浏览器,然后尝试在我的应用程序结束时再次关闭它。
只有我通过再次实例化网络驱动程序才重新打开浏览器。

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

这篇关于在selenium Web浏览器中获取浏览器关闭事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆