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?
The syntax for locating elements through XPath- Using contains() method can be written as: //<HTML tag>[contains(@attribute_name,'attribute_value')]
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.
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']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With