如何通过 watir-webdriver 获取 IE 的 WIN32OLE 句柄? [英] How to get WIN32OLE handle for IE via watir-webdriver?

查看:22
本文介绍了如何通过 watir-webdriver 获取 IE 的 WIN32OLE 句柄?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Watir 中,您可以使用 next 方法获取 IE 窗口的 WIN32OLE 句柄.

In Watir you can get WIN32OLE handle of IE window using next method.

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

我需要以某种方式为 watir-webdriver 创建的 IE 获得相同的回报.

I need somehow to get same return for IE that was created by watir-webdriver.

有什么办法吗?或者至少有人可以为我指明挖掘的方向.

Is there some way? Or at least someone can point me the direction to dig.

我需要这些东西来将 HTTPwatch 插件附加到我的浏览器实例.这是 HTTPWatch 代码的示例.

I need this stuff to attach HTTPwatch plugin to my browser instance. Here is example of HTTPWatch code.

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

UPD:感谢 Justin Ko,我有了可用的代码

UPD: Thanks to Justin Ko I have working code

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)

推荐答案

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

You could try using the WIN32OLE to attach to the running instance of IE. This was discussed on the Ruby On Windows blog - see here.

我认为您需要的代码是:

I think the code you would need is:

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

I do not have HttpWatch, so I was unable to test it. However, the win32ole type appears to be the same type as that returned by Watir's browser.ie().

请注意,此解决方案假定浏览器具有唯一的标题.如果这个假设无效,我可以写一些解决方法.

Note that this solution assumes that the browser has a unique title. If this assumption is not valid, I can write up some workarounds.

这篇关于如何通过 watir-webdriver 获取 IE 的 WIN32OLE 句柄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆