问题
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. How can I make the browser stop loading the page?
require 'watir-webdriver'
client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 10
@browser = Watir::Browser.new :chrome, :http_client => client
sites = [
"http://google.com/",
"http://yahoo.com/",
"http://www.nst.com.my/", # => This is the SLOW site
"http://drupal.org/",
"http://www.msn.com/",
"http://stackoverflow.com/"
]
sites.each do |url|
begin
@browser.goto(url)
puts "Success #{url}"
rescue
puts "Timeout #{url}"
end
end
########## Execution result ##########
# Success http://google.com/
# Success http://yahoo.com/
# Timeout http://www.nst.com.my/
# Timeout http://drupal.org/
# Timeout http://www.msn.com/
# Timeout http://stackoverflow.com/
########## Expected result ##########
# Success http://google.com/
# Success http://yahoo.com/
# Timeout http://www.nst.com.my/
# Success http://drupal.org/
# Success http://www.msn.com/
# Success http://stackoverflow.com/
It looks like the browser doesn't respond to any other commands before it finishes loading the page. How can I force the browser to throw away the page it is loading and execute the next command?
UPDATED
I have found an interesting capability flag loadAsync http://src.chromium.org/svn/trunk/src/chrome/test/webdriver/webdriver_capabilities_parser.cc Maybe it can be useful for solving this problem? I don't understand yet how to make watir (webdriver) to set this when starting the chromedriver. This flag was introduced here http://codereview.chromium.org/7582005/
回答1:
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
回答2:
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).
回答3:
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.
回答4:
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.
来源:https://stackoverflow.com/questions/9876529/make-headless-browser-stop-loading-page