XPath 查询获取 <a> 标签和 <b> 标签

XPath Query to get the <a> tag and <b> tag

我整天都在尝试使用 Xpath 查询从中获取值和标记,但我做不到。有人可以帮我处理我需要使用的 xpath 查询来获取它们吗?请参阅下面的 html 代码。

<html class="chrome webkit">
#shadow-root
<head>...</head>
<body id="jira" class="aui-layout aui-theme-default page-type-dashboard" data-version="6.1.2" data-aui-version="5.1.6">
<div id="page">
<header id="header" role="banner">...</header>
<div id="announcement-banner" class="alertHeader">
  <b> Production </b>
  <marquee scrollamoun="3" behaviour="alternate" onmouseover="this.stop()" onmouseout ="this.start()">..</marquee>
<#shadow-root
<font-color="red"> Note:Please check out 1.</font>
<a href="https://docs.google.com/a/query.com/document/" target="_blank">
 <b>
  <font color ="red"> GSD Service </font>
 </b>
</a>
</marquee>
</div>
<section id="content" role="main">...</section>
<footer id="footer" role="contentinfo">...</footer>
</div>
<div class="shim hidden"></div>
<div class="shim hidden"></div>
<div class="shim hidden"></div>
<div class="shim hidden"></div>
<div class="shim hidden"></div>
<div class="shim hidden"></div>
</body>
</html>

同样,我在这个标签之后还有另外三个 a 标签,所以我想分别获取所有 a 标签和 b 标签,以便在我的 application.Please 帮助我进行 XPath 查询。

假设您的 HTML 是 well-formed,下面的 XPath 将 select 所有 a 元素:

//a

就第一个a:

(//a)[1]

只是 div 中的第一个 a,其 @idpage:

(//div[@id='page']//a)[1]

您同样可以轻松地将这些概念应用于 selecting b

更新

以下 XPath 将 select 您在评论中指出的所有 a 个元素:

//div[@id='page']//div[@id='announcement-banner']//a[@target='_blank']

备注:

  • 虽然您的评论要求 target="_blank",但您的 a 发布HTML有target="_blank",所以你可能需要调整。
  • 如果您想要立即收容而不是任何深度的收容,请使用 / 而不是 //