Reputation: 969
I have a Perl script that uses Selenium to fetch a HTML document called foo that doesn't exist (404 not found.) The default behavior is for the script to print an error and terminate, such that "bar" is never printed. I'm looking for a way to get it to continue on instead, such that "bar" is printed.
Here's what I've got. Code (called foo.pl):
#!/usr/bin/perl
use strict;
use WWW::Selenium;
my $sel = WWW::Selenium->new( host => "localhost",
port => 4444,
browser => "*chrome",
browser_url => "http://www.google.com/" );
$sel->start;
$sel->open("http://www.google.com/foo.html")
print "bar";
Here's the message that is printed:
Error requesting http://localhost:4444/selenium-server/driver/?cmd=open&1=http%3A%2F%2Fwww.google.com%2Ffoo.html&sessionId=bc9c086eef804a7c8a0090674333e4c7:
XHR ERROR: URL = http://www.google.com/foo.html Response_Code = 404 Error_Message = Not Found
I've looked all over, but haven't found an answer to this. Thanks!
Upvotes: 0
Views: 1189
Reputation: 9303
Alternatively, you can create a (Test::)WWW::Mechanize object in your code (along with your Selenium object), feeding it the same URL that you want Selenium to check, and catching the status from the response object of the Mechanize object. Its a simple approach, I use it for HEAD requests that I issue before I do selenium GET requests.
(Will not work for javascript/ajax requests, though)
Upvotes: 0
Reputation: 2321
I'm not sure of the implementation in Perl, but in Java, Selenium-RC has a traffic capture mode, defined as selenium.start("captureNetworkTraffic=true");
that will enable you to capture HTTP responses, including error codes. Once the error code is retrieved, you can always resume execution...
Here is an excellent resource on how to capture and process/format this information once retrieved. It uses Python, though, but should give you a start.
Upvotes: 1