【问题标题】:How to get WIN32OLE handle for IE via watir-webdriver?如何通过 watir-webdriver 获取 IE 的 WIN32OLE 句柄?
【发布时间】:2012-04-16 09:02:17
【问题描述】:

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

【问题讨论】:

    标签: ruby watir watir-webdriver httpwatch


    【解决方案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() 返回的类型相同。

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

    【讨论】:

    • 您好,非常感谢您的帮助。您的代码没有工作,但稍作改动的版本效果很好。这里是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 puts WIN32OLE.new('Shell.Application').Windows(i).Document.Title puts $ie = WIN32OLE.new('Shell.Application').Windows(i) rescue end end controller = WIN32OLE.new('HttpWatch.Controller') plugin = controller.IE.Attach($ie)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-04
    • 1970-01-01
    • 2014-05-22
    • 1970-01-01
    • 2022-12-24
    • 2015-01-18
    相关资源
    最近更新 更多