94

As the title says, is it possible to select elements in XPath that only begin with a certain string, but perhaps do not end with the same?

For example there are 3 anchor elements:

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

I only want to get anchor elements that begin with 'buy.php/'. I don't think the following will work, will it:

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

How can I do this?

Nakilon
  • 34,866
  • 14
  • 107
  • 142
Allen Gingrich
  • 5,608
  • 10
  • 45
  • 57

2 Answers2

174

//a[starts-with(@href, 'buy.php/')]

http://www.zvon.org/xxl/XSLTreference/Output/function_starts-with.html

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
MooGoo
  • 46,796
  • 4
  • 39
  • 32
3

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/')]")

Michael Bazos
  • 227
  • 1
  • 1