Make headless browser stop loading page

前端 未结 4 1431
逝去的感伤
逝去的感伤 2021-02-15 12:29

I am using the watir-webdriver ruby gem. It starts the browser (Chrome) and begins to load a page. The page is loading too slowly, and watir-webdriver raises a timeout error. Ho

4条回答
  •  死守一世寂寞
    2021-02-15 12:50

    There are a couple of different ways to do what you're wanting, but here's what I would do:

    require 'watir-webdriver'
    
    client = Selenium::WebDriver::Remote::Http::Default.new
    client.timeout = 60 
    @browser = Watir::Browser.new :firefox, :http_client => client
    
    begin
      @browser.goto mySite
    rescue => e
      puts "Browser timed out: #{e}"
    end
    
    next_command
    

    If you have a lot of sites you're trying to load for confirmation of timeout or not, put them in an array:

    mySites = [
              mySite1,
              mySite2,
              mySite3
    ]
    
    mySites.each do |site|
       begin
          @browser.goto site
       rescue
          "Puts #{site} failed to load within the time allotted."
       end
    end
    

    UPDATE for proof of concept. This script always proceeds to step 2. The rescue isn't even necessary for the second goto, but is being used for clearer output. How is your script different than this?

    require 'watir-webdriver'
    
    client = Selenium::WebDriver::Remote::Http::Default.new
    client.timeout = 1     #VERY low timeout to make most sites fail
    @browser = Watir::Browser.new :firefox, :http_client => client
    
    
    def testing
       begin
         @browser.goto "http://www.cnn.com"
       rescue => e
         puts "Browser timed out on the first example"
       end
    
       begin
         @browser.goto "http://www.foxnews.com"
       rescue => e
         puts "Browser timed out on the second example"
       end
    end
    

提交回复
热议问题