---
title: "Watir Webdriver attach window"
description: "How to switch between windows in Watir Webdriver when the attach method is missing. Practical notes and code examples on."
url: "https://h3manth.com/scribe/watir-webdriver-attach-window/"
canonical_url: "https://h3manth.com/scribe/watir-webdriver-attach-window/"
markdown_url: "https://h3manth.com/scribe/blog/watir-webdriver-attach-window.md"
publishDate: "2012-04-03T00:00:00.000Z"
author: "Hemanth HM"
category: "ruby"
tags: ["ruby","testing"]
word_count: 129
reading_time_minutes: 1
x_markdown_tokens: 343
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# Watir Webdriver attach window

> How to switch between windows in Watir Webdriver when the attach method is missing. Practical notes and code examples on.

While porting a Watir based automation framework to watir-webdriver, I came across an issue that most have come across, that being: "In Watir and Selenium Webdrivers, there is no longer an attach method to attach to existing browsers."

But an in-depth analysis of the rspec for watir-webdriver brought a simple method to the lime light.

```ruby
browser.windows #=> [#<Window>, #<Window>]

browser.windows.first.current? #=> true

browser.windows.last.use

browser.windows.first.close

browser.window(:title => "foo").use

browser.window(:url => /bar/).use { ...code... }
```

As the above code makes it clear, it's easy to switch between windows, even though the 'attach' method is missing in the webdriver.

Alternatively one can also use:

```ruby
browser.driver.switch_to.window(browser.driver.window_handles[0])
```

But that would get complicated with too many popups to handle. Interestingly there is one [issue](http://code.google.com/p/selenium/issues/detail?id=18) still lurking around in the Selenium world!
