#!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');
Hello WWW::Mechanize::Firefox Goob bye
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']); } ####
$results = {
'nodeValue' => [
undef
],
'exception' => '',
'query' => '//p',
'textContent' => [
'Hello WWW::Mechanize::Firefox Goob bye'
],
'innerHTML' => [
'Hello WWW::Mechanize::Firefox Goob 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 expression 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 expression cannot be converted to return the specified type. at mech.pl line 28.
',
'query' => 'string-length(//p)',
'textContent' => undef,
'innerHTML' => undef
};