How to write xpath based on element's text in Robot Framework?

Anna picture Anna · Jan 23, 2017 · Viewed 22.7k times · Source

I am using the Robot Framework and Selenium2Library

The button has a text of "Save" and there is nothing more unique in it's xpath, so I'm trying to write an xpath based on element's text. How to write an xpath based on element's text with that piece of html:

<button class="slds-button slds-button--brand cuf-publisherShareButton NARROW uiButton" type="button" data-aura-rendered-by="1571:2954;a" data-aura-class="uiButton">
<span class=" label bBody truncate" dir="ltr" data-aura-rendered-by="1574:2954;a">Save</span>
</button>

(this is in the middle of the document).

EDIT: It appears that there are few elements with the same text on the next tab (which was invisible at the moment). How should I write the xpath for the second element with this text? I mean with index=1.

Answer

Goralight picture Goralight · Jan 23, 2017
Click Button    //button[.//text() = 'Save']

Is the "Robot Framework" way of finding a button with the text "Save" and clicking it.

Fixed from the help of @Tomalak <3