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

Hi Monks,

I am requesting all your wisdom and your skills ( normally just one or two skills not more :) ) please

I am trying to recover datas ( url's ) from my config file which is an excel file. Information's are in the first column of the sheet; For each data obtained, i would like to add the data, the URL in the adress bar of chrome to run with the browser.

Here is the code

#!/usr/bin/perl use strict; use warnings; use Test::More; #use Test::Time; use Selenium::Remote::Driver; use Selenium::Remote::WebElement; 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 $data = $excel->[1]{A1}; foreach my $data (0..9){ $driver->get($data); $driver->maximize_window(); #$driver->find_element('q','name')->send_keys($data); $driver->pause(2000); $driver->send_keys_to_active_element(KEYS->{'control'}, 't'); $driver->pause(2000); my $filename = "screenshot.png"; $driver->capture_screenshot($filename); $driver->pause(2000); } $driver->quit();
Here the output i get in my cmd
C:\Users\user1\Documents\TESTPERL>perl TEST6.pl Error while executing command: invalid argument: 'url' must be a strin +g (Session info: chrome=70.0.3538.77) (Driver info: chromedriver=2.43.600210 (68dcf5eebde37173d4027fa8635e +332711d2874a),platform=Windows NT 6.1.7601 SP1 x86_64) at C:/Strawber +ry/perl/sit e/lib/Selenium/Remote/Driver.pm line 391. at C:/Strawberry/perl/site/lib/Selenium/Remote/Driver.pm line 348.

What is going wrong please?

Is there an other (better) way to do it ? I know that TIMTOWTDI (Perl !!). I'm not sure, i am thinking about a hash or something like that but as i am a beginner i don't know how to do it exactly. Instead of having a simple loop and have to change the incrementation...

Thanks in advance

Replies are listed 'Best First'.
Re: Recover Excel data from cells with Foreach loop
by poj (Abbot) on Nov 10, 2018 at 18:18 UTC
    What is going wrong please?

    This ignores the spreadsheet and just gets 0 to 9

    foreach my $data (0..9){ $driver->get($data);

    Try

    #!/usr/bin/perl use strict; use Spreadsheet::Read; 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"; # selenium code etc }
    poj

      Thank you Poj ! But why is it ignoring the spreadsheet?

      I've checked and it's working fine, thx ! Otherwise, is there a way to do it with a loop, "foreach" or "for"?!

      Thanks again

        see foreach loops in perlsyn. Your foreach loop iterates over the list values of 0 to 9 and sets the scalar variable $data to be each element of the list in turn. Because the variable is preceded with the keyword my, then it is lexically scoped, and is therefore visible only within the loop.

        Try

        #!/usr/bin/perl use strict; use Spreadsheet::Read; my $excel = ReadData("TEST.csv"); my $maxrow = $excel->[1]{maxrow}; for my $row (1 .. $maxrow){ my $url = $excel->[1]{'A'.$row}; # do something with url process($url); } sub process { my ($url) = @_; print "Processing $url\n"; }
        poj