Make headless browser stop loading page

前端 未结 4 1433
逝去的感伤
逝去的感伤 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:44

    I've been fighting with this problem for awhile, I know that this post is from 2012 but I still haven't found anything that solves this issue.. So i made a work around.

    require 'watir-webdriver'
    
    client = Selenium::WebDriver::Remote::Http::Default.new
    client.timeout = 5
    @browser = Watir::Browser.new :firefox, :http_client => client
    
    @browser.goto "twitter.com"
    
    #after the page loads, log in 
    def test    
      begin
         temp = []
         temp = @browser.cookies.to_a
         @browser.goto "twitter.com:81"
      rescue => e
         puts "Browser timed out"
         @browser.close
         @browser = Watir::Browser.start "twitter.com"
    
         temp.each do |me|
           @browser.cookies.add(me[:name], me[:value])
         end
         @browser.refresh 
    
      end
    end
    

    The extra code that saves and restores your cookies will allow you to stay logged into sites you're using. This sucks but its the only work around I can think of. Again this was back in 2012, so if anyone finds anything that works better please correct me.

    0 讨论(0)
  • 2021-02-15 12:48

    You can stop Google loading the page using AutoIT to send the Escape key. This is similar to what you originally tried to do, but using AutoIT directly rather than through the broken Watir::Browser object.

    require 'watir-webdriver'
    require 'win32ole'
    
    client = Selenium::WebDriver::Remote::Http::Default.new
    client.timeout = 5
    @browser = Watir::Browser.new :chrome, :http_client => client
    begin
        @browser.goto "http://www.nst.com.my/"
    rescue
        autoit = WIN32OLE.new("AutoItX3.Control")
        autoit.AutoItSetOption("WinTitleMatchMode", 2) 
        autoit.WinActivate("Google")
        autoit.Send("{ESC}")
    end
    @browser.goto "http://www.google.ca"
    

    Note: I tried to get autoit.ControlSend("Google", "", "", "{ESC}") to work so that it would not need the browser to be the active window. While it worked when run by itself, for some reason I could never get it to work in the above script (ie key was sent but browser did not react as expected).

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-02-15 12:50

    I have had the problem of timeouts as well. My understanding from online research is that after Selenium WebDriver encounters a timeout, it gets into bad shape and misbehaves (multithreading issue). I have followed the advices found here:

    https://github.com/watir/watir-webdriver/issues/137

    I implemented active killing (instead of browser.close) and restarting of the Watir::Browser after any exception rescue.

    0 讨论(0)
提交回复
热议问题