1

在 Watir 中,您可以使用 next 方法获取 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:感谢 Justin Ko,我有工作代码

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)
4

1 回答 1

1

您可以尝试使用 WIN32OLE 附加到正在运行的 IE 实例。这在 Ruby On Windows 博客上进行了讨论 - 请参阅此处

我认为您需要的代码是:

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() 返回的类型相同。

请注意,此解决方案假定浏览器具有唯一的标题。如果这个假设不成立,我可以写一些变通方法。

于 2012-04-03T14:27:15.507 回答