9

One of the things that I miss about WatiN is the ability to attach to browser instances that were not spawned by Selenium. In my last position, this was the whole reason why I used WatiN, as the browser was always spawned by another application. I was hoping that I would be able to do this through .getWindowHandle() and then .SwitchTo().Window(string), but this doesn't seem as though it's the case.

The ability to do this is invaluable when it comes to writing and debuging scripts and do hope that I'm either doing something wrong, or that it's on the roadmap.

3 Answers 3

7

I am actually not entirely sure you can switch to a window not spawned by the driver. I think the people working on the selenium 2 project have worked a little bit on switching to a window spawned by a different driver. When you do:

driver.getWindowHandles()

All you get are the windows spawned by the driver object it is called on.

4
  • 1
    This is exactly right. There is no API for attaching to a running browser instance at present. It's a common feature request, though, and yes it's on the roadmap. It would be easiest to implement for IE, given the architecture of that particular driver. Remember that the "handle" you get from getWindowHandles() isn't a Windows HWND; rather it's an opaque string Selenium WebDriver uses to identify the window.
    – JimEvans
    May 19, 2011 at 15:54
  • Unfortunately, I've found this to be the case. Luckily, I don't need the functionality at the moment, however, anxiously await this ability in the future. Jun 6, 2011 at 10:12
  • 2016: Still not possible to attach to a browser not spawned by the driver? I'm trying to automate a complex login, still no luck.
    – zerologiko
    Jul 13, 2016 at 13:55
  • @zerologiko to my knowledge it is still not possible. If anyone would care to correct me that would be awesome.
    – Jason Ward
    Jul 14, 2016 at 15:11
1

You definitely won't be able to for Firefox (since selenium starts that browser with a selenium xpi ~ plugin file and is how it can control it).

I'm not sure about how IE is started, but I'm willing to bet it would be possible.

Not sure about chrome.

1
  • IE uses a dll and chrome uses a zip file.
    – Jason Ward
    May 10, 2011 at 20:07
0

It is indeed possible in Selenium 2. You could get window handle and switch to one on them. i.e.

for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);
}

For more on this look here.

2
  • I spent some time today trying to get this to work, but could only switch to handles of browsers that were opened during the current test execution. I'll sit down with one of my dev's though and see if I can't get this to work still. May 9, 2011 at 23:15
  • 1
    I do not think you can attach to a browser not spawned by the driver. Tarun's solution will only allow switching to and from browsers spawned by that particular driver
    – Jason Ward
    May 10, 2011 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.