no warnings;
use Selenium::Remote::Driver;
my $driver = Selenium::Remote::Driver->new;
$driver->get('https://www.crawler-test.com/');
$driver->find_element_by_xpath('//a[.="text not found"]');
How can I get the above code not to print this warning:
Error while executing command: no such element: Unable to locate element: //a[.="text not found"]
According to the docs, the function issues a "warning" if there is no element found, but having no warnings;
in the script does not suppress it.
How can I suppress this "warning"?
Use find_element
instead of find_element_by_xpath
. The former throws an exception instead of issuing a warning. You can catch these exceptions using the following wrappers:
sub nf_find_element {
my $node;
if (!eval {
$node = $web_driver->find_element(@_);
return 1; # No exception.
}) {
return undef if $@ =~ /Unable to locate element|An element could not be located on the page using the given search parameters/;
die($@);
}
return $node;
}
sub nf_find_elements {
my $nodes;
if (!eval {
$nodes = $web_driver->find_elements(@_);
return 1; # No exception.
}) {
return undef if $@ =~ /Unable to locate element|An element could not be located on the page using the given search parameters/;
die($@);
}
return wantarray ? @$nodes : $nodes;
}
sub nf_find_child_element {
my $node;
if (!eval {
$node = $web_driver->find_child_element(@_);
return 1; # No exception.
}) {
return undef if $@ =~ /Unable to locate element|An element could not be located on the page using the given search parameters/;
die($@);
}
return $node;
}
sub nf_find_child_elements {
my $nodes;
if (!eval {
$nodes = $web_driver->find_child_elements(@_);
return 1; # No exception.
}) {
return undef if $@ =~ /Unable to locate element|An element could not be located on the page using the given search parameters/;
die($@);
}
return wantarray ? @$nodes : $nodes;
}
nf
stands for "non-fatal".
Written for Selenium::Chrome, but should work with Selenium::Remote::Driver as well.