8

Hi there I tried to get all elements which contain href=/p/{random}/?tagged=see Here is my line

//div[preceding::h2[text()='Most recent']]/div/div/a[@href='/p/*/?tagged=see']

How I can fix this code, I must replace the '*' with something else?

Malasuerte94
  • 1,454
  • 3
  • 14
  • 18

1 Answers1

11

In XPath 2.0 or above, you can use Regex functions, for example :

//a[matches(@href, '/p/.*/\?tagged=see')]

Or using combination of string functions starts-with() and ends-with() :

//a[starts-with(@href, '/p/')]
   [ends-with(@href, '/?tagged=see')]

XPath 1.0 doesn't have regex nor ends-with() functions, however, you can simulate the latter :

//a[starts-with(@href, '/p/')]
   [substring(@href, string-length(@href) - string-length('/?tagged=see') +1) = '/?tagged=see']

Simplified :

//a[starts-with(@href, '/p/')]
   [substring(@href, string-length(@href) - 11) = '/?tagged=see']
Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137