John O
John O

Reputation: 5463

What is the minimum perl script for Selenium?

I've used homebrew to install the latest versions of geckodriver and chromedriver. I prefer the former, but would be willing to use either if they worked. I have installed with the cpan console the Selenium::Remote::Driver module as well, and it installed without any warnings. I am currently working from the example code snippets on https://metacpan.org/pod/Selenium::Remote::Driver .

use Selenium::Remote::Driver;
 
my $driver = Selenium::Remote::Driver->new;
$driver->get('http://www.google.com');
$driver->quit();

When I attempt to run the script, I receive the following error:

Selenium server did not return proper status at /Library/Perl/5.18/Selenium/Remote/Driver.pm line 544.

Now, I have no clue what I'm doing. And I'm working from some web sources that probably should be deprecated. Do I need to manually run geckodriver (or chromedriver) manually before I start this? If not, do I need to at least specify which to invoke in my code? Both are available in my path env. Is there some third component I've yet to install? A browser addon possibly?

My only goals are (at this point) to get it where it loads a web page into a browser (preferably not headless at this point in time, so that I can see it do what it does).

Upvotes: 3

Views: 1172

Answers (1)

ikegami
ikegami

Reputation: 386361

While Chrome-specific, the following is a minimal Selenium solution:

use FindBin          qw( $RealBin );
use Selenium::Chrome qw( );

my $web_driver = Selenium::Chrome->new(
   binary => "$RealBin/chromedriver.exe",
);

$web_driver->get('https://www.stackoverflow.com/');

$web_driver->shutdown_binary();

I wanted to handle exceptions, so I actually used this:

use FindBin             qw( $RealBin );
use Selenium::Chrome    qw( );
use Sub::ScopeFinalizer qw( scope_finalizer );

my $web_driver;
my $guard = scope_finalizer {
   if ($web_driver) {
      $web_driver->shutdown_binary();
      $web_driver = undef;
   }
};

$web_driver = Selenium::Chrome->new(
   binary => "$RealBin/chromedriver.exe",
);

$web_driver->get('https://www.stackoverflow.com/');

Upvotes: 4

Related Questions