Make headless browser stop loading page

空扰寡人 提交于 2019-12-03 12:52:09

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

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).

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.

tudeng

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!