Xpath to select html id including random number

Hi, how would I select all the links if they have the following identifier.

<a id="List_ctl01_link3" class="content" href=link1.aspx"> <a id="List_ctl02_link3" class="content" href=link2.aspx"> <a id="List_ctl03_link3" class="content" href=link3.aspx"> <a id="List_ctl04_link3" class="content" href=link4.aspx"> 

And so on...

Note that the last part of "link3" is important and should be included in the Xpath.

I am using the C # and html flexibility package.

+1
source share
3 answers

If you are using xpath 2.0, you can try the match / matches functions and use regular expressions. If you are using xpath 1.0, you probably have to write your own parsing parser (look at xsl: function). AFAIR compliance function is available only for xpath 2.0.

Probably @id [start-with (., 'List_ct') and ends (., 'Link3')] is another way to do this.

+2
source

Hi, how would I select the whole link when they have the next id

Use this XPath expression:

 //a[@id[starts-with(.,'List_ctl')][substring(.,string-length()-5)='_link3']] 

Note : in XPath 1.0 there is no fn:ends-with() . Use the last predicate instead.

+5
source

Using

 //a[@id[starts-with(.,'List_ctl') and substring(.,string-length()-5)='_link3' and floor(substring-before(substring_after(.,'List_ctl'),'_')) = floor(substring-before(substring_after(.,'List_ctl'),'_')) ] ] 

This XPath expression selects all a elements in the document, the id attribute has a string value with all of the following properties:

  • Runs the string 'List_ctl' .

  • Ends with the string '_link3' .

  • A substring surrounded by 'List_ctl' and '_' is a representation of an integer.

+3
source

Source: https://habr.com/ru/post/1495006/


All Articles