Reputation: 9
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">
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(), " 	
","")="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:
' ', ' ', ' ', '\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.
Upvotes: 0
Views: 565
Reputation: 4844
You could try this:
//button[normalize-space(translate(., ' ', ''))='Transactions']
Why?
Wel normalize-space()
does not get rid of the
and in xpath they can be removed with translate(., ' ', '')
Upvotes: 0
Reputation: 12672
The problem in that example is that text()
returns Transactions
(plus a couple of new lines) not Transactions
xmllint --html --xpath '//button/text()' test.html
Transactions
Adding single quotes to denote the real text:
echo "'$(xmllint --html --xpath '//button/text()[2]' test.html )'"
'
Transactions'
This comes a little closer
echo "'$(xmllint --html --xpath 'translate(normalize-space(//button/text()[2]), " ","")' test.html )'"
' Transactions'
Upvotes: 0
Reputation: 33361
Why not to locate the button based on it child img
node src
attribute value?
This should work:
//button[./img[contains(@src,'Transaction.png')]]
Upvotes: 1