Search code examples
perlxpathwww-mechanize

Pulling text from list items using WWW::Mechanize::Firefox


Given the following HTML:

<div class="chosen-drop">
  <ul class="chosen-results">
    <li>Stuff 1</li>
    <li>Stuff 2</li>
    <li>Stuff 3</li>
  </ul>
</div>

How do I pull the text from the list items using WWW::Mechanize::Firefox xpath function?

It seems like this should work, it's basically pulled from the documentation but it's coming up empty:

 my @text = $mech->xpath('//div[@class="chosen-drop"]/ul/li/text()');

I must be missing something with the xpath.


Solution

  • With these files:

    mech_xpath.pl:

    #!perl -w
    use strict;
    use WWW::Mechanize::Firefox;
    use Data::Dump qw/dump/;
    
    my $mech = WWW::Mechanize::Firefox->new();
    $mech->get_local('local.html');
    
    my @text = $mech->xpath('//div[@class="chosen-drop"]/ul/li/text()');
    warn dump \@text;
    
    <>;
    

    local.html:

    <div class="chosen-drop">
      <ul class="chosen-results">
        <li>Stuff 1</li>
        <li>Stuff 2</li>
        <li>Stuff 3</li>
      </ul>
    </div>
    

    Gives this output:

    [
      bless({
        # tied MozRepl::RemoteObject::TiedHash
      }, "MozRepl::RemoteObject::Instance"),
      bless({
        # tied MozRepl::RemoteObject::TiedHash
      }, "MozRepl::RemoteObject::Instance"),
      bless({
        # tied MozRepl::RemoteObject::TiedHash
      }, "MozRepl::RemoteObject::Instance"),
    ]
    

    So everything looks to be working. How are you checking the contents of @text?