Perlchaoui has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monastery !

I would like to get an answer if possible on the below topic. I have to test several url's and i would like to do some screnshot for each of them, once the website is available.

I can do a screnshot using this code. It's working fine.

my $filename = "screenshot.png"; $driver->capture_screenshot($filename);

But as the screenshot action is part of a loop ,when doing that, each screenshot is overriding the previous one because of the same path.

I searched and i did the following change but this is not working. I can load my script but i recover no screnshot

my $path = "C:/Users/user1/Documents/TESTPERL"; $driver->capture_screenshot("$path/Website-$url.png");

The output in CMD is here:

C:\Users\user1\Documents\TESTPERL>perl TEST7.pl Processing https://www.tricentis.com/ binmode() on closed filehandle $fh at C:/Strawberry/perl/site/lib/Sele +nium/Remote/Driver.pm line 1014. print() on closed filehandle $fh at C:/Strawberry/perl/site/lib/Seleni +um/Remote/Driver.pm line 1015. Processing https://learn.perl.org/examples/spreadsheet_read.html binmode() on closed filehandle $fh at C:/Strawberry/perl/site/lib/Sele +nium/Remote/Driver.pm line 1014. print() on closed filehandle $fh at C:/Strawberry/perl/site/lib/Seleni +um/Remote/Driver.pm line 1015. Processing https://github.com/mozilla/geckodriver/releases binmode() on closed filehandle $fh at C:/Strawberry/perl/site/lib/Sele +nium/Remote/Driver.pm line 1014. print() on closed filehandle $fh at C:/Strawberry/perl/site/lib/Seleni +um/Remote/Driver.pm line 1015.
And here the main code:
#!/usr/bin/perl use strict; use warnings; use Test::More; #use Test::Time; use Selenium::Remote::Driver; use Selenium::Remote::WebElement; use Selenium::Remote::WDKeys; use Spreadsheet::Read; use Text::CSV_XS; my $driver = Selenium::Remote::Driver->new( 'remote_server_addr' => 'localhost', 'browser_name' => 'chrome', 'port' => '4444', ); my $excel = ReadData("TEST.csv"); my $row = 1; my $url = $excel->[1]{'A'.$row}; while ($url){ # do something with url process($url); ++$row; $url = $excel->[1]{'A'.$row}; } sub process { my ($url) = @_; print "Processing $url\n"; $driver->get($url); $driver->maximize_window(); $driver->pause(2000); #my $filename = "screenshot.png"; #$driver->capture_screenshot($filename); my $path = "C:/Users/user1/Documents/TESTPERL"; $driver->capture_screenshot("$path/Website-$url.png"); $driver->pause(2000); }

I already put this in a post but it was a remark part of another topic.

Can someone help on this ?

Many thanks

Replies are listed 'Best First'.
Re: Screenshot with Perl
by poj (Abbot) on Nov 12, 2018 at 10:13 UTC

    Are you trying to create a file with a name like this ?

    C:/Users/user1/Documents/TESTPERL/Website-https://www.tricentis.com/.png

    Try just using the row numbers

    $driver->capture_screenshot("$path/Website-$row.png");

    poj

      Thaank you Poj ! You are a killer ( in the good sense of the term)

      With your support, i understood now from where is coming the issue. Obviously, the png doc cannot store the url name as is

      Again thanks for the support. I just don't know why in IT University we start to learn at the beginning Java or C languages and not Perl which is a powerful , useful and beautiful language..

        With your support, i understood now from where is coming the issue.

        Sometimes it's very useful to simply see what you're doing. A humble print statement can help:

        my $path = "C:/Users/user1/Documents/TESTPERL"; ... my $url = some_func(...); ... my $filename = "$path/Website-$url.png"; print "DEBUG: screenshot filename: '$filename'"; # FOR DEBUG only $driver->capture_screenshot($filename); ...
        Had you seen this output during debugging, it would probably have been immediately obvious that "... the png doc cannot store the url name as is".

        See also the Basic debugging checklist; these techniques will allow you to support yourself.


        Give a man a fish:  <%-{-{-{-<

        I just don't know why in IT University we start to learn at the beginning Java or C languages and not Perl which is a powerful , useful and beautiful language..

        I think this would make a great perlmonks poll, because there are so many provocative reasons for this sad state of affairs (sad for the grads, that is =).

        You are a killer ( in the good sense of the term)

        Off topic, just chiming in to say, generally in the sense you mean, in US English, killer is an adjective not a noun. So, you are killer is more on target and not in need of qualification. Maybe the expression killer app is muddying the idiom.

Re: Screenshot with Perl
by Discipulus (Canon) on Nov 12, 2018 at 10:16 UTC
    Hello Perlchaoui

    use the module URI to break the url and use parts you needs (update: and discard unneeded parts that breaks filename creation):

    use URI; ... my $url = shift; # https://www.tricentis.com/ my $uri = URI->new( $url ); my $filename = $uri->host().'.png'; #or whatever you prefere

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Hello Discipulus

      A big Thank you. It's working fine. I wasn't aware about this module. Really useful.

      Just another remark . How can i add a link to a user when mentioning him in a post? In a comment for Perlmonks?