Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: running an example script with WWW::Mechanize* module

by Corion (Pope)
on Apr 19, 2020 at 06:28 UTC ( #11115779=note: print w/replies, xml ) Need Help??


in reply to Re^2: running an example script with WWW::Mechanize* module
in thread running an example script with WWW::Mechanize* module

There is a misunderstanding of $mech->success - this method only reflects whether the last HTTP response from the server is considered an error or not. It does not reflect whether the last operation on $mech was successful or not. Error checking is usually done by die by WWW::Mechanize::Chrome.

I haven't run your code, but the log output suggests that the form you're looking at has no name:

say $mech->current_form->{name}; # ?? # Use of uninitialized value in say at ./5.pluto.pl line 55.

The form is not great, because it really contains three fields with the same name date, so you will have to fetch the individual fields and explicitly set them:

# largely untested my @date_fields = $mech->selector('.//*[@name="date"]', node => $self- +>current_form ); $mech->set_field( $date_fields[1] => $guess );

In the next version, I'll actually implement the arrayref form of ->set_fields() for values of index larger than one :) But that means breaking my (incompatible) API to restore the WWW::Mechanize API so I'll have to look carefully there.

$mech->set_fields( $name => [ 'foo', 2 ] );

Replies are listed 'Best First'.
Re^4: running an example script with WWW::Mechanize* module
by Aldebaran (Deacon) on Apr 21, 2020 at 03:22 UTC
    The form is not great, because it really contains three fields with the same name date, so you will have to fetch the individual fields and explicitly set them:

    Thanks, Corion, I think we're almost there. I've got this pared down as far as I can to make an SSCCE. I can't get perl to think I have a valid selector:

    $ ./6.1.pluto.pl say (...) interpreted as function at ./6.1.pluto.pl line 47. 2020/04/20 20:10:39 Connected to ws://127.0.0.1:40757/devtools/browser +/10f7d706-4dbd-4073-a438-916f6602fb4c found the one and only form Invalid rule, couldn't parse '//*[@name="date", node => $mech->current +_form ]' at /usr/local/share/perl/5.26.1/HTML/Selector/XPath.pm line +283. $

    Source, with the critical line tried several different ways:

    #! /usr/bin/perl use warnings; use strict; use WWW::Mechanize::Chrome; use Log::Log4perl qw(:easy); use Data::Dump; use 5.016; Log::Log4perl->easy_init($INFO); my $site = 'https://www.fourmilab.ch/cgi-bin/Yoursky?z=1&lat=45.5183&ns=North&lon +=122.676&ew=West'; my $mech = WWW::Mechanize::Chrome->new( headless => 1, ); $mech->get($site); my $guess = 2458960; #Earth day 2020 in julian days $mech->form_number(1); say "found the one and only form" if $mech->success(); # my best guess...aka...trial 1 # $mech->field( date => '2', jd => $guess ); # stderr: 3 elements found for input with name 'date' at ./6.1.pluto.p +l line 21. # your first guess...aka trial 2 # my @date_fields =$mech->selector( './/*[@name="date"]', node => $sel +f->current_form ); # stderr: Global symbol "$self" requires explicit package name # 3rd guess #my @date_fields =$mech->selector( './/*[@name="date"]', node => $mech +->current_form ); # stderr: Invalid rule, couldn't parse '//*[@name="date"]' at /usr/loc +al/share/perl/5.26.1/HTML/Selector/XPath.pm line 283. # 4th guess # my @date_fields =$mech->selector( './/*[@name="date", node => $self- +>current_form ]'); # stderr: Invalid rule, couldn't parse '//*[@name="date", node => $sel +f->current_form ]' # 5th guess my @date_fields =$mech->selector( './/*[@name="date", node => $mech->c +urrent_form ]'); # Invalid rule, couldn't parse '//*[@name="date", node => $mech->curre +nt_form ]' $mech->set_field( $date_fields[1] => $guess ); $mech->click_button( value => "Update" ); # this seems similar to W +M say "clickbutton succeeded" if $mech->success(); my $string = $mech->uri; say ("We are at $string") if $mech->success();

    That lays it out there as starkly as I can. VielenDank und Gruss aus Amiland.

      Invalid rule, couldn't parse '//*[@name="date", node => $mech->current +_form ]'

      This looks as if you're mixing Perl code and an XPath expression in the same string.

      my @date_fields =$mech->selector( './/*[@name="date"]', node => $mech +->current_form ); # stderr: Invalid rule, couldn't parse '//*[@name="date"]' at /usr/loc +al/share/perl/5.26.1/HTML/Selector/XPath.pm line 283.

      This is weird - I think this should still parse as an XPath expression. Does the following work and give the expected result?

      my @date_fields =$mech->xpath( './/*[@name="date"]', node => $mech->c +urrent_form );

      With version 0.48 of WWW::Mechanize::Chrome, ->set_fields now also supports selecing input elements by their name and index:

      $mech->set_fields( date => [3, $date_value], );

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11115779]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2020-11-27 06:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?