dfaure has asked for the wisdom of the Perl Monks concerning the following question:
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...
|
---|
Back to
Seekers of Perl Wisdom