Home > Blockchain >  XPath 1.0 expression to select an Element with text and child nodes
XPath 1.0 expression to select an Element with text and child nodes

Time:03-02

I've been having serious issues detecting elements in a particular section of a document. The issue is regarding a large menu presented as a sequence of buttons that contain both image and text in that order to this one:

<button type="button" id="ext-gen375" >
    <img style="height:13px" src="inc/FAST/images/icons/Transaction.png">
    &nbsp;&nbsp;&nbsp;Transactions
</button>

I want to select the button using its text contents, the issue is that there are other buttons that have similar names, i.e."Policy" and "Policy Address". The ideal solution would be to match the text avoiding the use of contains or other substring functions, but I've been struggling to do so. I have tried several different expressions that seem fine on http://xpather.com/ but do not work on Mozilla or Chrome at all.

//button[text()[normalize-space()="Transactions"]]
//button[normalize-space(text())="Transactions"]
//button[normalize-space(.)="Transactions"]
//button[text()[translate(normalize-space(), "  &#13;&#10;&#09;&#xA;","")="Transactions"]]

Prophet had an excellent suggestion to use the tag in the search. Unfortunately, similar buttons share the same icon.

Thanks in advance guys.

CodePudding user response:

Why not to locate the button based on it child img node src attribute value?
This should work:

//button[./img[contains(@src,'Transaction.png')]]

CodePudding user response:

The problem in that example is that text() returns &nbsp;&nbsp;&nbsp;Transactions (plus a couple of new lines) not Transactions

xmllint --html --xpath '//button/text()' test.html 

    
    &nbsp;&nbsp;&nbsp;Transactions

Adding single quotes to denote the real text:

echo "'$(xmllint --html --xpath '//button/text()[2]' test.html )'"
'
    &nbsp;&nbsp;&nbsp;Transactions'

This comes a little closer

echo "'$(xmllint --html --xpath 'translate(normalize-space(//button/text()[2]), " ","")' test.html )'"
'   Transactions'

CodePudding user response:

You could try this:

//button[normalize-space(translate(., '&#160;', ''))='Transactions']

Why? Wel normalize-space() does not get rid of the &nbsp; and in xpath they can be removed with translate(., '&#160;', '')

  • Related