给出以下HTML:
<div class="chosen-drop">
<ul class="chosen-results">
<li>Stuff 1</li>
<li>Stuff 2</li>
<li>Stuff 3</li>
</ul>
</div>
如何使用WWW::Mechanize::Firefox xpath
function?
看起来这应该有用,它基本上是从文档中提取出来的,但它是空的:
my @text = $mech->xpath('//div[@class="chosen-drop"]/ul/li/text()');
我必须遗漏xpath。
答案 0 :(得分:1)
使用这些文件:
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>
给出这个输出:
[
bless({
# tied MozRepl::RemoteObject::TiedHash
}, "MozRepl::RemoteObject::Instance"),
bless({
# tied MozRepl::RemoteObject::TiedHash
}, "MozRepl::RemoteObject::Instance"),
bless({
# tied MozRepl::RemoteObject::TiedHash
}, "MozRepl::RemoteObject::Instance"),
]
所以一切看起来都有效。你如何检查@text
的内容?