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

I am adding one more method which uses HTML::TreeBuilder::XPath to select what you want using an XPath expression. Mainly for sentimental reasons because TreeBuilder helped me out in many a cases, but also because XPath expressions are useful to know and use.

A word of warning: HTML::TreeBuilder seems to ignore unknown-to-it html tags by default, e.g. HTML5 tags like section. A quick but dirty fix is to instruct it to lax a bit, see Issues using HTML::TreeBuilder::XPath and the HTML5 <section> tag

Here is example code tested with your html string:

#!/usr/bin/env perl use strict; use warnings; use HTML::TreeBuilder::XPath; my $html = '<select class="c-form__select" id="radius" name="radius" a +ria-label="Choose a distance from your postcode"><option value="">Dis +tance (national)</option><option value="1">Within 1 mile</option><opt +ion value="5">Within 5 miles</option><option value="10">Within 10 mil +es</option><option value="15">Within 15 miles</option><option value=" +20">Within 20 miles</option><option value="25">Within 25 miles</optio +n><option value="30">Within 30 miles</option><option value="35">Withi +n 35 miles</option><option value="40">Within 40 miles</option><option + value="45">Within 45 miles</option><option value="50">Within 50 mile +s</option><option value="55">Within 55 miles</option><option value="6 +0">Within 60 miles</option><option value="70">Within 70 miles</option +><option value="80">Within 80 miles</option><option value="90">Within + 90 miles</option><option value="100">Within 100 miles</option><optio +n value="200">Within 200 miles</option></select>'; my $TB = HTML::TreeBuilder::XPath->new( # if you have html5 with tags unknown to 'HTML::TreeBuilder' then +uncomment following # ignore_unknown => 0, ); $TB->parse($html) || die 'TB->parse()'; #my $xpath = '//form[@name="searchVehicles"]//select[@name="radius"]/o +ption[@value!=""]/@value'; my $xpath = '//select[@name="radius"]//option[@value!=""]/@value'; my @values = $TB->findvalues($xpath); print "found ".scalar(@values)." option values:\n"; foreach my $avalue (@values){ print "value: '".$avalue."'\n"; }

p.s. Personally, I would use a package which can cope better with latest HTML standards. XPath for me is appealing, say like a regex is. However, if what you are building is going to be a long term, expanding project you need to maintain - easily - your XPaths whenever site's web design changes. Maybe then Mojo::DOM (which I have not tried) is the way to go.