XPath ordered priority attribute search

Posted by user94000 on Stack Overflow See other posts from Stack Overflow or by user94000
Published on 2009-04-21T21:01:26Z Indexed on 2010/05/08 21:38 UTC
Read the original article Hit count: 248

Filed under:
|
|
|

I want to write an XPath that can return some link elements on an HTML DOM.

The syntax is wrong, but here is the gist of what I want:

//web:link[@text='Login' THEN_TRY @href='login.php' THEN_TRY @index=0]

THEN_TRY is a made-up operator, because I can't find what operator(s) to use. If many links exist on the page for the given set of [attribute=name] pairs, the link which matches the most left-most attribute(s) should be returned instead of any others.

For example, consider a case where the above example XPath finds 3 links that match any of the given attributes:

link A: text='Sign In', href='Login.php', index=0
link B: text='Login', href='Signin.php', index=15
link C: text='Login', href='Login.php', index=22

Link C ranks as the best match because it matches the First and Second attributes.

Link B ranks second because it only matches the First attribute.

Link A ranks last because it does not match the First attribute; it only matches the Second and Third attributes.

The XPath should return the best match, Link C.

If more than one link were tied for "best match", the XPath should return the first best link that it found on the page.

© Stack Overflow or respective owner

Related posts about xpath

Related posts about attribute