How to get WIN32OLE handle for IE via watir-webdriver?

巧了我就是萌 提交于 2019-12-23 02:16:28

问题


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

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

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.

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

回答1:


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)

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.



来源:https://stackoverflow.com/questions/9980888/how-to-get-win32ole-handle-for-ie-via-watir-webdriver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!