gpt4 book ai didi

testing - watir webdriver try catch 代理

转载 作者:行者123 更新时间:2023-11-28 21:31:51 38 4
gpt4 key购买 nike

以下是连接代理与 watir webdriver 的工作代码:

b = Watir::Browser.new :chrome, :switches => %w[--proxy-server=xxx.xxx.xx.xxx:80]

现在,如果代理不工作,我怎样才能捕获它并尝试使用其他代理?比如 try and catch 还是 cases?

最佳答案

看起来您只能通过转到页面来确定代理是否有效。如果 Chrome 无法连接到代理,它会显示一条消息“无法连接到代理服务器”。因此,您可以尝试:

1) 使用代理访问页面 2)检查消息 3) 如果出现该消息,请尝试使用其他代理 4) 如果消息没有出现,则代理有效

例如,以下将尝试第一个代理,但会失败。然后它会去尝试下一个代理,等等。

proxy_servers = ['111.111.11.111:80', '222.222.22.222:80']

browser = nil

proxy_servers.each do |proxy|
browser = Watir::Browser.new :chrome, :switches => ["--proxy-server=#{proxy}"]

# Try going to a page
browser.goto 'http://www.google.ca'

#If Chrome says "Unable to connect to the proxy server", try another one
if browser.text.include?('Unable to connect to the proxy server')
browser.close
else
break
end
end

# Throw an exception if a valid proxy server cannot be found
raise 'No valid proxy servers found' unless browser.exists?

关于testing - watir webdriver try catch 代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18127319/

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