2013-03-03 110 views
6

我写了几个类来管理我想要如何处理多个网站,并使用类似的方法(即登录,刷新)。每个类都打开自己的WATIR浏览器实例。Watir的红宝石线程

class Site1 
    def initialize 
     @ie = Watir::Browser.new 
    end 
    def login 
     @ie.goto "www.blah.com" 
    end 
end 

的代码在主样本有没有线程是如下

require 'watir' 
require_relative 'site1' 

agents = [] 
agents << Site1.new 

agents.each{ |agent| 
    agent.login 
} 

这工作得很好,但直到当前已完成登录犯规移动到下一个代理。我想整合多线程来处理这个问题,但似乎无法让它工作。

require 'watir' 
require_relative 'site1' 

agents = []; threads = [] 
agents << Site1.new 


agents.each{ |agent| 
    threads << Thread.new(agent){ agent.login } 
} 

threads.each { |t| t.join } 

这给了我错误:未知属性或方法:navigate。 HRESULT错误代码:0x8001010e。该应用程序称为一个接口,被编组为另一个线程。

有谁知道如何解决这个问题,或者如何实现类似的功能?

+1

它似乎是watir-classic或其中一个库使用的错误。在Firefox中使用watir-wedriver时,问题不会发生。 – 2013-03-04 17:25:24

+0

感谢贾斯汀,它看起来像你是对的。 watir-webdriver运行良好,所以我猜它只是一个watir-classic的bug。 – cubesnyc 2013-03-04 21:37:44

回答

0

对此不太确定,但这里是使用线程的摆动。

require 'thread' 
    threads = []    # Setting an array to store threaded commands 
    c_thread = Thread.new do # Start a new thread 
    login      # Call our command in the thread 
    end 
    threads << c_thread