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

Hello Perl Monks,

i want to write a little script to grab and play around with the results of the autocomplete of a website.
So I want to work with Selenium::Firefox, that base on module https://metacpan.org/pod/Selenium::Remote::Driver, but the description of the methods is without any examples.

I have this basic example, that is able to open google and insert a search string.
Then you can see that a result list is suggested and i want to get this list.
But i have no idea how this can be obtained?

Here is my code so far:
#!/usr/bin/perl use strict; use warnings; use Selenium::Firefox; my $mech = Selenium::Firefox->new( startup_timeout => 20, firefox_binary => '/srv/bin/firefox.62.0/firefox', binary => '/usr/local/bin/geckodriver', marionette_enabled => 1 ); my $search = "perl"; my $url = "https://www.google.com/"; $mech->get($url); $mech->find_element_by_name("q"); sleep(3); my $lastname = $mech->get_active_element(); $lastname->send_keys($search); sleep (10); $mech->shutdown_binary; exit 0;
I could find no examples to use this Perl module - and there are more questions for it.
As for instance: https://metacpan.org/pod/Selenium::Remote::Driver#find_element
How can i turn on the warnings instead of killing the script?
Or how can i step through the objects of the wep page?

The description is not understandable for people who are not experts.
But my hope is that experts can give me a hint?

Replies are listed 'Best First'.
Re: Get autocomplete result with Selenium::Remote::Driver
by bliako (Abbot) on Sep 09, 2018 at 12:11 UTC
    find_element
    
     Description:
        Search for an element on the page, starting from the document
        root. The located element will be returned as a WebElement
        object. If the element cannot be found, we will CROAK, killing
        your script. If you wish for a warning instead, use the
        parameterized version of the finders:
    
            find_element_by_class
            ... etc.
    
    (from Selenium::Remote::Driver)

    Which does exist in your script ($mech->find_element_by_name("q");), so does it get killed? Not on my side.

    Secondly, there may be some confusion about how these elements are specified. I.e. the parameter to find_element*($string) should it be css, xpath? The warning I got initially for:

    Error while executing command: invalid argument: Unknown locator strat +egy css at /usr/local/share/perl5/Selenium/Remote/Driver.pm line 391. at /usr/local/share/perl5/Selenium/Remote/Driver.pm line 348. at /usr/local/share/perl5/Selenium/Remote/Finders.pm line 26.

    disappeared when I used:

    $mech->find_element_by_xpath('//input[@name="q"]');

    Setting the constructor parameter default_finder can be of help.

    WWW::Mechanize::Chrome can also be of use here.

    Btw, url to get google autocomplete for 'perl'.

    https://www.google.com/complete/search?client=psy-ab&gs_rn=64&gs_ri=psy-ab&cp=3&gs_id=jv&q=perl&xhr=t

    Btw but irrelevant: notice the client=psy-ab in the above url.

    bw, bliako