Skip to content

Instantly share code, notes, and snippets.

@gempesaw
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save gempesaw/0473b78f43c1a7654d7c to your computer and use it in GitHub Desktop.

Select an option

Save gempesaw/0473b78f43c1a7654d7c to your computer and use it in GitHub Desktop.
exceptions
#! /usr/bin/perl
use strict;
use warnings;
use feature qw/say/;
use Selenium::Remote::Driver;
my $d = Selenium::Remote::Driver->new;
$d->get('http://www.google.com');
# This doesn't crash anything: client is alive, browser is alive
$d->find_elements('missing', 'css');
# creates the following in the webdriver log: no exceptions
# 14:52:46.426 INFO - Executing: [find elements: By.selector: missing])
# 14:52:46.535 INFO - Done: [find elements: By.selector: missing]
# can still talk to our current session
say $d->get_title;
use Try::Tiny;
try {
# this will crash only the client, not the browser/server.
$d->find_elements('this is an invalid xpath expression', 'xpath');
# webdriver log, this takes the eval/croak path
# 14:52:46.547 INFO - Executing: [find elements: By.xpath: this is an invalid xpath expression])
# 14:52:47.270 WARN - Exception thrown
# org.openqa.selenium.InvalidSelectorException: The given selector this is an invalid xpath expression is either invalid or does not result in a WebElement. The following error occurred:
# InvalidSelectorError: Unable to locate an element with the xpath expression this is an invalid xpath expression because of the following error:
# SyntaxError: The expression is not a legal expression.
}
catch {
say 'Browser is still alive, client is only alive because of try/catch';
say $d->get_title;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment