选择其属性以 XPath 中的某个元素开头的元素

正如标题所说,有没有可能在 XPath 中选择只以某个字符串开头,但可能不以相同字符串结尾的元素?

例如,有3个锚元素:

<a href="buy.php/onething"></a><a href="buy.php/twothing"></a><a href="sell.php/anotherthing"></a>

我只想得到以‘ buy.php/’开头的锚元素:

getByXPath("//a[@href='buy.php/']")

我怎么能这么做?

52483 次浏览

Not sure if this is exactly the correct syntax but you probably want to use the fn:contains xpath function. Other useful functions you can find here:

http://www.w3schools.com/xpath/xpath_functions.asp#string

getByXPath("//a[fn:contains(@href/text(), 'buy.php/')]")