Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress "warning" using Selenium::Remote::Driver?

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"?

like image 485
CJ7 Avatar asked Mar 02 '23 06:03

CJ7


2 Answers

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.

like image 184
ikegami Avatar answered Mar 05 '23 16:03

ikegami


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.

That's right. The warnings pragma is lexical. Adding no warnings to your code will only affect your code. It won't turn off warnings in other modules that your code uses. As the documentation says:

This pragma works just like the "strict" pragma. This means that the scope of the warning pragma is limited to the enclosing block. It also means that the pragma setting will not leak across files (via "use", "require" or "do"). This allows authors to independently define the degree of warning checks that will be applied to their module.

like image 21
Dave Cross Avatar answered Mar 05 '23 15:03

Dave Cross