Anna
Anna

Reputation: 157

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

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.

Upvotes: 1

Views: 36642

Answers (6)

Goralight
Goralight

Reputation: 2107

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

Upvotes: 3

Hemant Kulkarni
Hemant Kulkarni

Reputation: 193

Try this -

//span[text()='Save']/ancestor-or-self::button

Upvotes: 0

Rakesh
Rakesh

Reputation: 1575

Try using below xpath:

 xpath=//span[contains(text(),'Save')]

Upvotes: 0

Akash W
Akash W

Reputation: 371

Use following keyword, which will first verify whether element is present or not and then it will click on element located by locator

Element should become visible    xpath=//button/span[text()='Save']
Click Button     xpath=//button/span[text()='Save']

There is another built in keyword

Click Element    xpath=//button/span[text()='Save']

Upvotes: 0

NarendraR
NarendraR

Reputation: 7708

Use following xpath to find a button which have text as save -

 //button[contains(.,'Save')]

Upvotes: 0

Cathal
Cathal

Reputation: 1338

Try searching for a button which contains a span with your required text

  WebElement saveButton = driver.findElement(By.xpath(".//button/span[text()='Save']")

Upvotes: 1

Related Questions