【问题标题】:watir webdriver try catch proxy等待 webdriver 尝试捕获代理
【发布时间】:2013-08-08 13:29:16
【问题描述】:

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

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

现在,如果代理不起作用,我如何才能抓住它并尝试使用其他代理? 比如 try and catch 还是 with case?

【问题讨论】:

    标签: testing watir watir-webdriver


    【解决方案1】:

    您似乎只能通过转到某个页面来确定代理是否有效。如果 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?
    

    【讨论】:

      猜你喜欢
      • 2021-06-10
      • 2021-05-05
      • 2014-12-01
      • 1970-01-01
      • 2019-10-20
      • 2020-08-06
      • 2018-04-20
      • 2019-10-24
      • 2017-05-17
      相关资源
      最近更新 更多