Perl with Selenium: cannot save the Web page with Ctrl+S

51 Views Asked by At

I tried to download an entire HTML from the given URL with Perl Selenium::Chrome. My scheme is:

  • open the Web page
  • press Ctrl+S to open the Save As box
  • press Alt+S to accept the default file location / name and save it

I ran the code attached below but I cannot save it. When I tried "no headless" mode (visible mode), I found:

  • the browser opened specified Web page
  • no save box opened
  • no save action was done
  • closed safely without errors nor saved files

How can I make it?

#!/usr/bin/env perl

# seleniumTest.pl

use strict;
use warnings;
use Selenium::Chrome;
use Data::Dumper;
use Selenium::Remote::WDKeys;
use Selenium::Remote::Driver;
use Selenium::ActionChains;

my $url = 'https://www.example.com/foo';
my $profile_path = '/home/cf/.config/google-chrome'; # this is to use my own google account info
my $profile_name = 'Profile 1'; # ditto

my $driver = Selenium::Chrome->new (
    extra_capabilities => {
    'goog:chromeOptions' => {
        args => ['user-data-dir='.$profile_path, 'profile-directory='.$profile_name,
             #   'headless', 'disable-gpu', 'window-size=1920,1080', 'no-sandbox' # if you want to do it headless, decomment this line
        ],
        #binary => '/mnt/c/Users/cf/Downloads/chrome-headless-shell-linux64/chrome-headless-shell' # ditto
    }
    }
    );

$driver->set_implicit_wait_timeout(5000);

$driver->get($url); # the browser opens if you don't set the headless mode

warn $driver->get_title(); # This works fine so I believe the selenium works

sleep 10;
warn "opened";
my $html = $driver->find_element("/html");

my $action_chains = Selenium::ActionChains->new(driver => $driver);
$action_chains->key_down( [ KEYS->{'control'}], $html); # I am not sure that it is ok to specify <html> as the element...
$action_chains->send_keys('s');
$action_chains->key_up( [ KEYS->{'control'}], $html);
sleep 10;
warn "try to save";

$action_chains->key_down( [ KEYS->{'alt'}], $html);
$action_chains->send_keys('s');
$action_chains->key_up( [ KEYS->{'alt'}], $html);
warn "saved?";
sleep 10;
$driver->shutdown_binary;
warn "ended";
0

There are 0 best solutions below