2016-02-11 99 views
0

考慮下面的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

我懷疑你沒有顯示完整的HTML文檔。其中可能有一個名稱空間。 –

回答

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的內容?

+1

好的,頁面上有一些jquery生成列表項。當我運行我的腳本時他們不在那裏。 – StevieD