属性值以特定字符串结尾的元素的XPath?

Hap*_*ird 6 html xml xpath ends-with

鉴于HTML包含:

  <div tagname="779853cd-355b-4242-8399-dc15f95b3276_Destination" class="panel panel-default"></div>
Run Code Online (Sandbox Code Playgroud)

我们如何在XPath中编写以下表达式:

查找属性以字符串'Destination'结尾的<div>元素tagname

我一直在寻找几天,我无法想出一些有用的东西.在众多中,我试过例如:

div[contains(@tagname, 'Destination')]
Run Code Online (Sandbox Code Playgroud)

kjh*_*hes 8

XPath 2.0

//div[ends-with(@tagname, 'Destination')]
Run Code Online (Sandbox Code Playgroud)

XPath 1.0

//div[substring(@tagname,string-length(@tagname) -string-length('Destination') +1) 
      = 'Destination']
Run Code Online (Sandbox Code Playgroud)

  • 1.0版本救了我,谢谢。 (2认同)

Iev*_*gen 6

您可以使用ends-with(Xpath 2.0)

//div[ends-with(@tagname, 'Destination')]
Run Code Online (Sandbox Code Playgroud)


Bil*_*ell 6

XPath 2 或 3:总是有正则表达式。

.//div[matches(@tagname,".*_Destination$")]
Run Code Online (Sandbox Code Playgroud)