newspaint

Documenting Problems That Were Difficult To Find The Answer To

Installing Selenium for Perl on Ubuntu 16.04

First I had to download the “Selenium Standalone Server” (a Java .jar file) from the Selenium download page (version 3.5.3 as of writing this article).

Then I ensured I had a JRE (Java run-time environment) by executing:

~$ sudo apt-get install openjdk-9-jre

I also downloaded the Mozilla GeckoDriver from the download page specified above (version 0.18.0 as of writing this article). I then extracted geckodriver-v0.18.0-linux64.tar.gz (the geckodriver executable) into the same folder as the Java Selenium Standalone Server .jar.

~$ cd /opt/selenium
~$ tar -xvjf geckodriver-v0.18.0-linux64.tar.gz

I started up the Java Selenium Standalone Server in a different terminal window by running:

~$ cd /opt/selenium
~$ $ java -jar selenium-server-standalone-3.5.3.jar
2017-09-02 04:35:00.772:INFO::main: Logging initialized @601ms to org.seleniumhq.jetty9.util.log.StdErrLog
2017-09-02 04:35:00.973:INFO:osjs.Server:main: jetty-9.4.5.v20170502
2017-09-02 04:35:00.991:WARN:osjs.SecurityHandler:main: ServletContext@o.s.j.s.ServletContextHandler@77f1baf5{/,null,STARTING} has uncovered http methods for path: /
2017-09-02 04:35:00.995:INFO:osjsh.ContextHandler:main: Started o.s.j.s.ServletContextHandler@77f1baf5{/,null,AVAILABLE}
2017-09-02 04:35:01.012:INFO:osjs.AbstractConnector:main: Started ServerConnector@87a85e1{HTTP/1.1,[http/1.1]}{0.0.0.0:4444}
2017-09-02 04:35:01.012:INFO:osjs.Server:main: Started @845ms

Next I installed a package for Perl support of Selenium by downloading the CPAN package Selenium-Remote-Driver v1.20 (25 May 2017) and extracting the file Selenium-Remote-Driver-1.20.tar.gz and running the following commands:

~$ sudo apt-get install libwww-perl libarchive-zip-perl libfile-which-perl libio-string-perl libjson-perl libmoo-perl libxml-simple-perl libtry-tiny-perl libsub-install-perl libtest-longstring-perl libnamespace-clean-perl make
~$ cd /opt/selenium
~$ tar -xvjf Selenium-Remote-Driver-1.20.tar.gz
~$ cd Selenium-Remote-Driver-1.20
~$ perl -w Makefile.PL
~$ make

Then I wrote a test script:

#!/usr/bin/perl -w

use lib '/opt/selenium/Selenium-Remote-Driver-1.20/lib';
use Selenium::Remote::Driver;

use strict;

print( "- Connecting to Selenium Server Standalone (Java)...\n" );
my $driver = Selenium::Remote::Driver->new(
  'remote_server_addr' => "localhost",
  'port' => 4444,
  'browser_name' => 'firefox',
);

print( "- Opening Google webpage...\n" );
$driver->get( "http://www.google.com/" );

print( "- Display webpage title on console...\n" );
print $driver->get_title() . "\n";

sub find_and_visit() {
  print( "- Search for 'newspaint'...\n" );
  my $elem = $driver->find_element( "//input[\@name='q']" );
  if ( ! $elem ) {
    print( "Could not find input element with name 'q'.\n" );
    return;
  }

  $elem->send_keys( "newspaint" );
  sleep( 2 );

  my $submit = $driver->find_element(
    "//input[\@value='Google Search']"
  );
  if ( ! $submit ) {
    print( "Could not find Google Search button.\n" );
    return;
  }

  $submit->click();
  sleep( 5 );

  my @anchors = $driver->find_elements( "//a" );
  foreach my $anchor ( @anchors ) {
    my $href = $anchor->get_attribute( "href" );
    next if ( ! $href );

    if ( $href eq "https://newspaint.wordpress.com/" ) {
      print( "Clicking on newspaint blog link...\n" );
      $anchor->click();
      sleep( 5 );
      last;
    }
  }
}

find_and_visit();

print( "Quitting...\n" );
$driver->quit();

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: