how to get already opened IE browser handle in selenium?

你离开我真会死。 提交于 2019-12-14 01:15:35

问题


I have one already opened IE browser , with some url. After this, I Run below code which will open another IE browser. however it gives me only one window handle in below code. Is it possible to get previously opened IE browser handle ?

        IWebDriver IEdriver = new InternetExplorerDriver();
        IReadOnlyCollection<String> browsers = IEdriver.WindowHandles;
        foreach (String item in browsers)
        {
            IEdriver.SwitchTo().Window(item);
            String url = IEdriver.Url;

        }

回答1:


I think this is what you're looking for:

String winHandleBefore = driver.getWindowHandle();
//Do whatever operations you have to do
for(String winHandle : IEdriver.getWindowHandles()){
IEdriver.switchTo().window(winHandle);
}



回答2:


Be careful as what you are trying to do, is not robust solution to write tests cases. If one test case causes browser to crash you will be getting all the test cases failed.

Also I don't think it should be possible to get handles on previously opened window by default, because when you write code

    IWebDriver IEdriver = new InternetExplorerDriver();

It calls for a constructor of InternetExplorerDriver class and opens new instance of Internet Explorer.




回答3:


You can either go for close all browsers before starting your test case execution by killing the ie process from task manager.

        foreach (Process process in Process.GetProcessesByName("iexplore"))
        {
            process.Kill();
        }


来源:https://stackoverflow.com/questions/47172927/how-to-get-already-opened-ie-browser-handle-in-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!