gpt4 book ai didi

Ruby 线程与 Watir

转载 作者:数据小太阳 更新时间:2023-10-29 07:08:32 25 4
gpt4 key购买 nike

我编写了几个类来控制我想如何处理多个网站,两者都使用类似的方法(即登录、刷新)。每个类都打开自己的 WATIR 浏览器实例。

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

无线程的main中的代码示例如下

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。应用程序调用了为不同线程编码的接口(interface)。

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

最佳答案

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

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

关于Ruby 线程与 Watir,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15189678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com