Hi,

I'm having an annoying issue with WWW::Mechanize::Firefox xpath() method. The following code...

#!perl -w use strict; use WWW::Mechanize::Firefox; use Data::Dumper; my $mech = WWW::Mechanize::Firefox->new(activate => 1); $mech->autoclose_tab(0); $mech->update_html(<<'HTML'); <html> <head> <title>Hello Firefox!</title> </head> <body> <h1>Hello <b>World</b>!</h1> <p id='paragraph'>Hello <b>WWW::Mechanize::Firefox</b> Goob bye</p> </body> </html> HTML test_xpath($mech, '//p', all => 1); test_xpath($mech, '//p/text()', all => 1); test_xpath($mech, 'substring(//p,1,4)', all => 1); # expected String: +Hell test_xpath($mech, 'string-length(//p)', all => 1); # expected Number: +38 sub test_xpath { my ($mech, $xpq, %opts) = @_; my @xpr; eval { @xpr = $mech->xpath($xpq, %opts); }; my %results = ( query => $xpq, exception => $@, innerHTML => scalar(@xpr) ? [ map { $_->{innerHTML} } @xpr ] : + undef, textContent => scalar(@xpr) ? [ map { $_->{textContent} } @xpr ] : + undef, nodeValue => scalar(@xpr) ? [ map { $_->{nodeValue} } @xpr ] : + undef ); print Data::Dumper->Dump([\%results], ['results']); }

...shows that (sadly for now in v0.71) not all the Xpath results are handled by this nevertheless amazing module.

$results = { 'nodeValue' => [ undef ], 'exception' => '', 'query' => '//p', 'textContent' => [ 'Hello WWW::Mechanize::Firefox Goob by +e' ], 'innerHTML' => [ 'Hello <b>WWW::Mechanize::Firefox</b> Go +ob bye' ] }; $results = { 'nodeValue' => [ 'Hello ', ' Goob bye' ], 'exception' => '', 'query' => '//p/text()', 'textContent' => [ 'Hello ', ' Goob bye' ], 'innerHTML' => [ undef, undef ] }; $results = { 'nodeValue' => undef, 'exception' => 'MozRepl::RemoteObject: TypeError: The exp +ression cannot be converted to return the specified type. at mech.pl +line 28. ', 'query' => 'substring(//p,1,4)', 'textContent' => undef, 'innerHTML' => undef }; $results = { 'nodeValue' => undef, 'exception' => 'MozRepl::RemoteObject: TypeError: The exp +ression cannot be converted to return the specified type. at mech.pl +line 28. ', 'query' => 'string-length(//p)', 'textContent' => undef, 'innerHTML' => undef };

I would be very interested with any workaround.

____
HTH, Dominique
My two favorites:
If the only tool you have is a hammer, you will see every problem as a nail. --Abraham Maslow
Bien faire, et le faire savoir...


In reply to Some issues with WWW::Mechanize::Firefox->xpath() method by dfaure

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.