Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: How do I pull HTML form option values?

by NetWallah (Canon)
on Apr 03, 2018 at 17:48 UTC ( [id://1212271]=note: print w/replies, xml ) Need Help??


in reply to How do I pull HTML form option values?

Are you looking for something like this?:
use strict; use warnings; my $v= [ bless( { 'class' => 'c-form__select', 'current' => 0, 'id' => 'radius', 'menu' => [ { 'name' => 'Distance (national)', 'value' => '1', 'seen' => 1 } ], 'type' => 'option', 'aria-label' => 'Choose a distance from your postco +de', 'name' => 'radius', 'idx' => 1 }, 'HTML::Form::ListInput' ), bless( { }, 'HTML::Form::ListInput' ), bless( { }, 'HTML::Form::ListInput' ), bless( { }, 'HTML::Form::ListInput' ) ]; my $LookForName = 'radius'; for my $entry (grep {$_->{name} and $_->{name} eq $LookForName} @$v) +{ print "Found $LookForName:\n"; for my $menuItem (@{ $entry->{menu} }){ print "\tValue:",$menuItem->{value},"\n"; } }

                Memory fault   --   brain fried

Replies are listed 'Best First'.
Re^2: How do I pull HTML form option values?
by unklejunky (Initiate) on Apr 03, 2018 at 18:56 UTC
    Not quite. Here's the form input field for `radius` (Distance):
    select class="c-form__select" id="radius" name="radius" aria-label="Ch +oose a distance from your postcode"><option value="">Distance (nation +al)</option><option value="1">Within 1 mile</option><option value="5" +>Within 5 miles</option><option value="10">Within 10 miles</option><o +ption value="15">Within 15 miles</option><option value="20">Within 20 + miles</option><option value="25">Within 25 miles</option><option val +ue="30">Within 30 miles</option><option value="35">Within 35 miles</o +ption><option value="40">Within 40 miles</option><option value="45">W +ithin 45 miles</option><option value="50">Within 50 miles</option><op +tion value="55">Within 55 miles</option><option value="60">Within 60 +miles</option><option value="70">Within 70 miles</option><option valu +e="80">Within 80 miles</option><option value="90">Within 90 miles</op +tion><option value="100">Within 100 miles</option><option value="200" +>Within 200 miles</option></select>

    (https://www.autotrader.co.uk/)

    I'm after all the values of the `value=` tags for each `<option>`, i.e. 5,10,20,30 etc into an array which I can then use elsewhere in the code. Perhaps find_all_inputs() wasn't the correct way to capture this?
      From what I can tell, WWW::Mechanize uses HTML::Form, which , in my opinion, does not parse the <select> tag properly.

      The code looks for "multiple":

      if (exists $self->{multiple}) { unshift(@{$self->{menu}}, { value => undef, name => "off"} +); $self->{current} = $checked ? 1 : 0; } else { $self->{current} = 0 if $
      which does not seem to be set anywhere.
      As a result, it only pickes up the first <option>.

      You will likely have to parse the raw html using HTML::TokeParser or the like, to extract the options.

      See fellow un-answered sufferer at stackoverflow.

                      Memory fault   --   brain fried

        From what I can tell, WWW::Mechanize uses HTML::Form, which , in my opinion, does not parse the <select> tag properly. ... excerpt from source code

        Hi,

        I doubt rt://HTML-Form has issue with select tag.

        On the aforementioned website, the distances in miles are provided by javascript.

        HTML::Form, like WWW::Mechanize, doesn't run javascript. For that you'd need WWW::Mechanize::Firefox /..::Chrome/..::PhantomJS

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (8)
As of 2024-03-28 11:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found