例如,如何通过 xpath 选择文本strong
后的标签?baz
<p>
<br>foo<strong>this foo</strong>
<br>bar<strong>this bar</strong>
<br>baz<strong>this baz</strong>
<br>qux<strong>this qux</strong></p>
显然以下不起作用....
//p[text() = 'baz']/following-sibling::select[1]
例如,如何通过 xpath 选择文本strong
后的标签?baz
<p>
<br>foo<strong>this foo</strong>
<br>bar<strong>this bar</strong>
<br>baz<strong>this baz</strong>
<br>qux<strong>this qux</strong></p>
显然以下不起作用....
//p[text() = 'baz']/following-sibling::select[1]
试试这个
//p/text()[. = 'baz']/following-sibling::strong[1]
在这里演示 - http://www.xpathtester.com/obj/b67bad4d-4d38-4e2d-a3df-b7e5a2e9f286
此解决方案不依赖于文本节点周围的空白。如果您开始使用缩进或其他空白字符,您将需要切换到使用以下内容
//p/text()[normalize-space(.) = 'baz']/following-sibling::strong[1]