簡體   English   中英

使用XPath選擇以下兄弟的href屬性

[英]Using XPath to select the href attribute of the following-sibling

我試圖刮掉以下網站: http//www.hudson211.org/zf/profile/service/id/659837

我正在嘗試選擇“網址”文本旁邊的href。 以下xpath選擇器獲取我之后的標記:

$x("//th[contains(text(), 'Web Address')]/following-sibling::td/a")

回報

<a href="http://www.co.sullivan.ny.us">www.co.sullivan.ny.us</a>

但是,當我專門嘗試使用@href提取href時,返回值是一個空數組:

$x("//th[contains(text(), 'Web Address')]/following-sibling::td/a/@href")

返回[]

這是我正在查看的行的html:

<tr valign="top">
    <td class="profile_view_left"></td>
    <th align="left" class="profile_view_center">Web Address</th>
    <td class="profile_view_right">
      <ahref="http://www.co.sullivan.ny.us">www.co.sullivan.ny.us</a>                         </td>
    <td></td>
</tr>

我假設你使用的是谷歌Chrome控制台,因為這個$x()函數。 選擇@href屬性的xpath 確實有效 ,正如我在Chrome中測試的那樣,只有結果不會像控件中那樣顯示在控制台中 - 這是我目前不太確定的原因 - :

>var result = $x("//th[contains(text(), 'Web Address')]/following-sibling::td/a/@href")
undefined
>result[0].value
"http://www.co.sullivan.ny.us"

看到使用完全相同的表達式,變量result包含預期的url值。 如果您的目的只是在控制台中顯示單個href值而無需進一步處理,則會執行以下操作:

>$x("//th[contains(text(), 'Web Address')]/following-sibling::td/a/@href")[0].value
"http://www.co.sullivan.ny.us"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM