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

Given following HTML:

<div id="CID60" class="calendar"> <div> <h2 class="">City Council Subcommittee</h2> </div> <ol class=""> <li class=""> <h3> <a id="eventTitle_2941" href="" onclick="eventDetails(2941, +3, 4, 2016, 0); return false;" name="eventTitle_2941"><span class=""> City Council Sub-Committee Legislative and Ordinance Commi +ttee</span> </a> </h3> <a href="/Calendar.aspx?EID=2941&amp;day=3&amp;month=4&amp;yea +r=2016&amp;calType=0" class=""> More Details </a> </li> <li class=""> <h3> <a id="eventTitle_2921" href="" onclick="eventDetails(2921, +3, 4, 2016, 0); return false;" name= "eventTitle_2921"><span class="" +> City Council Sub-Committee Personnel Action Committee</spa +n> </a> </h3> <a href="/Calendar.aspx?EID=2921&amp;day=3&amp;month=4&amp;yea +r=2016&amp;calType=0" class=""> More Details </a> </li> </ol> </div>

I'm trying to extract the href values for the two links with a text of "More Details" using the xpath method. Here's the code I'm using to do that:

my @links = $mech->xpath('//div[@id=\'CID60\']/ol/li/a/@href', type => + $mech->xpathResult('STRING_TYPE', all => 1)); print Dumper(\@links);

This is what gets returned:

$VAR1 = [ '/Calendar.aspx?EID=2941&day=3&month=4&year=2016&calType=0', '' ];

I'm not sure why the second value is empty. This xpath tester says my xpath argument should work fine.

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: Having trouble getting WWW::Mechanize::Firefox->xpath to return expected results
by Corion (Patriarch) on Apr 03, 2016 at 19:32 UTC

    Most likely it is something with your use of xpathResult('STRING_TYPE'.... Also, there is no ->mech() method in WWW::Mechanize::Firefox.

    Personally, I would recommend simply retrieving the links and extracting the href attribute on the Perl side:

    my @links = ...('//div[@id="CID60"]/ol/li/a', all => 1); my @href = map { $_->{href} } @links;

      Thanks Corion!

      The mech method was a bad edit on my part. Fixed.

      So your code worked, but I'm not quite sure how or why $_->{href} extracted the href property from @links. When I dump @links I get some really hairy looking objects. That's why I tried converting them to a string in the first place.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
      $nysus = $PM . ' ' . $MCF;
      Click here if you love Perl Monks

        The objects look so hairy because they live in Firefox and have some Perl code that fetches the values from Firefox.