If this tool helped you, you can buy us a coffee ☕
在浏览器里直接测试XPath表达式,立刻看到匹配结果。适合爬虫、数据采集、前端调试。
One prefix mapping per line, in the format prefix=URI. Default namespaces must be referenced using a custom prefix in XPath.
Please enter content and an XPath expression to see results.

MBTI Career Personality Test
Take the 28-question MBTI personality assessment to get your 4-letter type code and discover your career tendencies.

Kasiski Examination Tool
Estimate the key length of classical ciphers like the Vigenère cipher by analyzing repeated ciphertext sequences to assist in decryption.

Regular Expression Tester
Test regular expressions online, validate matches in real-time, and quickly extract text data.

Online Keyboard Tester
Test your keyboard for broken keys and ghosting issues. A free hardware diagnostic tool to check key functionality and responsiveness.

MBTI Career Personality Test
Take the 28-question MBTI personality assessment to get your 4-letter type code and discover your career tendencies.

Kasiski Examination Tool
Estimate the key length of classical ciphers like the Vigenère cipher by analyzing repeated ciphertext sequences to assist in decryption.

Regular Expression Tester
Test regular expressions online, validate matches in real-time, and quickly extract text data.

Online Keyboard Tester
Test your keyboard for broken keys and ghosting issues. A free hardware diagnostic tool to check key functionality and responsiveness.

Keyboard APM Test
Measure your effective keystrokes per minute to evaluate typing speed and accuracy. Ideal for programmers and writers.
写爬虫想提取网页里所有商品价格的链接,或者调试自动化测试脚本里的元素定位,又或者只是好奇某段HTML里某个标签到底有几个——用XPath可以一句话定位。但XPath写对了没?表达式容易手滑。我们的XPath在线测试工具让你贴一份HTML/XML文档,写上XPath,立即显示匹配数量和高亮内容,省去反复刷新页面或打开开发者工具的麻烦。
XPath是一种在XML文档里找节点的路径语言,类似文件系统的目录路径。比如//div[@class='price']表示“所有class属性等于price的div元素”。把它想象成给文档里的元素一个“地址”,你用这个地址就能直接拿到它们。开发者经常用它来从网页里提取数据,或者控制自动化流程。
//a/@href表示提取所有超链接的地址。假设你有一段简单的商品HTML(粘贴到文档框):
<div id="products">
<div class="item">
<span class="name">无线鼠标</span>
<span class="price">¥39.9</span>
</div>
<div class="item">
<span class="name">机械键盘</span>
<span class="price">¥129</span>
</div>
</div>你想提取所有商品名称。在XPath框输入://span[@class='name']/text()。点击测试,结果区域会显示“匹配到2个节点”,并列出:
1. 无线鼠标
2. 机械键盘
假如你想一次性拿到名称和价格,可以用联合选择或返回元素的父节点,但初学者先用简单表达式。
再看一个对照例:如果你写//span[@class='price'](不加/text()),匹配结果会显示“2个节点”,但每个节点是<span class="price">¥39.9</span>这样的整个标签——因为XPath默认返回节点,不是文本。如果你的目的是拿数字,记得加/text()或string()。
//tag 往往不匹配,需要先注册命名空间前缀。我们的工具目前只支持无命名空间或显式前缀的匹配,遇到命名空间时请先手动移除或改用local-name() 函数。<Div> 和 <div> 是两个不同的节点,写表达式时要注意。// 导致性能问题:虽然工具处理小文档很快,但在真实大文档里滥用 // 会扫描全部节点,建议用具体路径如 /html/body/div[1]/ 来提高效率。//div [@class=...](方括号前有空格)是语法错误,工具会提示“Invalid expression”。写表达式时不要乱加空格。text() 能取到所有文本:text() 只返回直接文本子节点,嵌套元素里的文本不会自动合并。要取全部文本可以用 string() 或 normalize-space()。local-name() 或先去除命名空间声明。@class='value' 但实际属性值前后有空格。可以先用 //* 看看所有元素,再逐步缩窄。contains() 函数吗?//div[contains(@class, 'price')] 可以匹配 class 属性包含“price”的 div。//a 返回所有 a 元素,然后结果框会显示节点的文本(inner text)。要拿 href 属性,用 //a/@href,但注意返回的是属性节点,文本为空。可以同时写两个表达式分两次测试,或者用多表达式功能(如果工具有提供)。[object Text] 之类的,不是我要的文本/text() 或 /string() 可以拿到字符串。现在你可以在上方的输入框里试试自己的 HTML 和 XPath 表达式了。