Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing XPath expressions matching a prefix

Tags:

xpath

I am fairly new to XPath.

The requirement is to create a XPath expression which returns a list of elements, rather than a single element. After that we apply custom logic to locate the required element in the list.

The individual element can be accessed using the below XPaths:

.//*[@id='reportListItemID0']/td[12]/a
.//*[@id='reportListItemID1']/td[12]/a
.//*[@id='reportListItemID2']/td[12]/a
.
.
.
.//*[@id='reportListItemID20']/td[12]/a
.//*[@id='reportListItemID21']/td[12]/a

Please let me know, how to write a XPath expression which would returns all the above elements.

I tried the below and they did not work:

.//*[@id='reportListItemID.']/td[12]/a
.//*[@id='reportListItemID..']/td[12]/a
.//*[@id='reportListItemID*']/td[12]/a
.//*[@id='reportListItemID**']/td[12]/a

Please let me know what I am missing.

like image 267
Vel Ganesh Avatar asked Feb 16 '23 19:02

Vel Ganesh


2 Answers

You should use the starts-with() function:

.//*[starts-with(@id,'reportListItemID')]/td[12]/a
like image 139
MiMo Avatar answered Mar 24 '23 11:03

MiMo


You can also use contains:

//*[contains(@id, 'reportListItemID')]/td[12]/a
like image 22
Nora Avatar answered Mar 24 '23 12:03

Nora