简体   繁体   中英

Finding currently open browser (firefox) windows with Watir

I'm trying to write a script that will look for and use a browser window (it can be either IE or Firefox) that is already opened before the script is executed. The attach method works for IE, but it can't with Firefox.

browser = Watir::Browser.attach(:url, /url.com/)

Using Watir-Webdriver I tried the window switching trick posted elsewhere, but that only seems to work when you clicked a link from the original browser window. It can't seem to find a window that was already opened before the script is run.

The user community on blogs elsewhere tell me this is only possible with IE and using watir and not watir-webdriver. There's an issue opened at the selenium issue tracker for webdriver, but it's been open for quite a while.

I'm hoping there's a workaround out there. Any ideas?

Webdriver不支持附加到现有进程,因此也不能使用watir-webdriver

I found this when looking for a solution for the same problem.

Something I came up with was to write a very simple Ruby webservice using Sinatra, and let that handle the browser object as a global variable:

require 'rubygems'
require 'sinatra'
require 'watir-webdriver'

set :port, 9000

get '/openbrowser' do
    $browser = Watir::Browser.new :ff
    $timeout_length = 30    
    $browser.driver.manage.window.maximize
end

get '/closebrowser' do 
    $browser.close
end

Then have a second script send HTTP requests to the webservice like this:

require 'net/http'
require 'uri'

url = "http://localhost:9000/openbrowser" 
uri = URI.parse(url)
Net::HTTP.get(uri)
sleep(5)

url = "http://localhost:9000/closebrowser" 
uri = URI.parse(url)
Net::HTTP.get(uri)

So, with this you can open a browser, and use it with independent scripts, as long as you want, and close it out when you're done.

Bad news is: it would probably take a lot of rework for you if you already have something.

Good news is, if you're starting from scratch, you can get it built quickly. And, if you store your browser objects in an array, you could test against multiple browsers in parallel if you're into that sort of thing.

Hope this helps (although it's 3 years late)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM