http://qs321.pair.com?node_id=11115856


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

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.

Replies are listed 'Best First'.
Re^5: running an example script with WWW::Mechanize* module
by Corion (Patriarch) on Apr 21, 2020 at 06:07 UTC
    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], );