Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xpath get a if href contains part of string

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?

like image 758
Malasuerte94 Avatar asked Mar 20 '17 10:03

Malasuerte94


People also ask

How to use and in XPath with contains?

The syntax for locating elements through XPath- Using contains() method can be written as: //<HTML tag>[contains(@attribute_name,'attribute_value')]

How use attribute for contain in XPath?

Using the XPath contains() function, we can extract all the elements on the page that match the provided text value. Here, tag: tag is the name of the tag that contains the specific word. word: In this case, the word refers to the text that must be discovered in a specific string.


1 Answers

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']
like image 176
har07 Avatar answered Nov 15 '22 03:11

har07