0

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" class=" x-btn-text">
    <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"]]

Thanks in advance guys.

Edit1:

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

Edit2

Based on Siebe's answer I was able to look a deep further into the situation. My goal was to have a working XPath 1.0 expression for automation in Selenium, but I was using Chrome and Firefox to test the expressions. For some reason on those browsers, Non-Breaking Spaces in a text will not match the common whitespace character or any of the characters bellow:

 '&nbsp;', '&#160;', '&#xA0;', '\u00a0'

After many hours trying to find something, I opted to start a Selenium WebDriver section and perform the tests there and to my surprise, the XPath bellow worked using the Chrome Driver:

//button[text()[translate(., "\u00a0", "")="Transactions"]]

While that expression works for Selenium automation projects, it really reinforces the impact that different implementations of the same tool can have on your project. Thanks again to everyone that replied.

3 Answers 3

1

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

//button[./img[contains(@src,'Transaction.png')]]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for answering! Images are reused in different buttons. I will add that to the post.
I see.. What's about the button id attribute id="ext-gen375"? Is it unique and static or not unique or dynamic?
0

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'

Comments

0

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;', '')

1 Comment

Thank You, Siebe. That almost hit the mark. I found a solution based on yours and will update the original question to with the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.