2012-04-02 46 views
1

在Watir中,您可以使用下一個方法獲得IE窗口的WIN32OLE句柄。如何通過watir-webdriver爲IE獲取WIN32OLE句柄?

irb(main):059:0> browser.ie 
=> #<WIN32OLE:0x28d12b8> 

我需要以某種方式獲得由watir-webdriver創建的IE的相同返回。

有什麼方法嗎?或者至少有人能指出我要挖掘的方向。

我需要這個東西來將HTTPwatch插件附加到我的瀏覽器實例。這裏是HTTPWatch代碼的例子。

require 'watir' 
require 'win32ole' 
browser = Watir::Browser.new 
controller = WIN32OLE.new('HttpWatch.Controller') 
plugin = controller.IE.Attach(browser.ie) 

UPD:由於官高我有工作代碼

require 'win32ole' 
require 'watir-webdriver' 

browser = Watir::Browser.new :ie 
title = browser.title 
browser.goto "google.com" 

length = WIN32OLE.new('Shell.Application').Windows.count - 1 

(0..length).each do |i| 
begin 
     WIN32OLE.new('Shell.Application').Windows(i).Document.Title 
     $ie = WIN32OLE.new('Shell.Application').Windows(i) 
    rescue 
    end 
end 

controller = WIN32OLE.new('HttpWatch.Controller') 
plugin = controller.IE.Attach($ie) 

回答

1

你可以嘗試使用WIN32OLE附加到IE的運行實例。這在Ruby On Windows博客上進行了討論 - 請參閱here

我認爲你需要的代碼是:

require 'win32ole' 
require 'watir-webdriver' 

browser = Watir::Browser.new :ie 
title = browser.title 

for window in WIN32OLE.new('Shell.Application').Windows 
    begin 
     if window.Document.Title == title 
      ie = window 
     end 
    rescue 
    end 
end 

controller = WIN32OLE.new('HttpWatch.Controller') 
plugin = controller.IE.Attach(ie) 

我沒有HttpWatch的,所以我無法測試。但是,win32ole類型看起來與Watir的browser.ie()返回的類型相同。

請注意,此解決方案假定瀏覽器具有唯一標題。如果這個假設無效,我可以寫一些解決方法。

+0

嗨,非常感謝您的幫助。你的代碼不起作用,但有點改變的版本很好。這是 '需要 'WIN32OLE' 需要 '的Watir-webdriver的' 瀏覽器=的Watir :: Browser.new:即 標題= browser.title browser.goto 「google.com」 長度= WIN32OLE.new ('Shell.Application')。Windows.count - 1 (0..length).each do | i | \t開始 提出WIN32OLE.new( 'Shell.Application')。視窗(I).Document.Title 把$即= WIN32OLE.new( 'Shell.Application')。視窗(I) 救援 結束 結束 controller = WIN32OLE.new('HttpWatch.Controller') plugin = controller.IE.Attach($ ie)' – 2012-04-05 13:59:09