how to get already opened IE browser handle in sel

2019-06-23 19:12发布

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;

        }

3条回答
来,给爷笑一个
2楼-- · 2019-06-23 19:26

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楼-- · 2019-06-23 19:31

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();
        }
查看更多
Lonely孤独者°
4楼-- · 2019-06-23 19:43

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);
}
查看更多
登录 后发表回答