1

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.

Anna
  • 147
  • 1
  • 1
  • 13

6 Answers6

3
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

Goralight
  • 2,067
  • 6
  • 25
  • 40
  • Unfortunatelly I got error: Element locator 'xpath=//button[contains(text(), 'Save')]' did not match any elements. – Anna Jan 23 '17 at 12:17
  • This is unlikely to work, because `text()` selects all child text nodes, and `text() = 'Save'` compares the first of them (!) to the string "Save". The first text child of the ` – Tomalak Jan 23 '17 at 12:18
  • better, and what you probably wanted to express: `//button[.//text() = 'Save']`, which compares all text node descendants of all ` – Tomalak Jan 23 '17 at 12:19
  • Yeah I messed up - Leaving answer here as a refernece for the future as works within my own suite. Cheers @Tomalak – Goralight Jan 23 '17 at 12:30
  • That makes no sense :) Just correct it and it will be fine. With XPath there are many ways to express the same thing, so why not have an answer that shows an alternative? – Tomalak Jan 23 '17 at 12:32
  • Was going to leave it for now as busy with something else but will edit now :3 I meant instead of deleting it ill edit it. – Goralight Jan 23 '17 at 12:34
  • I am still getting ElementNotVisibleException: error. Maybe it's something wrong with this element? – Anna Jan 23 '17 at 12:49
  • @Anna Compare http://stackoverflow.com/questions/27927964/selenium-element-not-visible-exception – Tomalak Jan 23 '17 at 13:07
  • @Tomalak 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 index=1 – Anna Jan 27 '17 at 14:55
  • @Anna Look into the `position()` XPath function. You will find plenty of examples and you'll figure it out, I'm sure. – Tomalak Jan 27 '17 at 14:57
  • Hi What would be the solution if the span has class. `````` – vgdub Apr 05 '21 at 10:12
1

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

  WebElement saveButton = driver.findElement(By.xpath(".//button/span[text()='Save']")
Cathal
  • 1,318
  • 1
  • 12
  • 19
0

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

 //button[contains(.,'Save')]
NarendraR
  • 7,577
  • 10
  • 44
  • 82
  • This one gaves me error: ElementNotVisibleException – Anna Jan 23 '17 at 12:20
  • 2
    Not recommendable because it can lead to unintended false positives. It would match a ``, which is not the OP's intent. – Tomalak Jan 23 '17 at 12:21
0

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']
Akash W
  • 371
  • 1
  • 10
0

Try using below xpath:

 xpath=//span[contains(text(),'Save')]
Rakesh
  • 1,525
  • 1
  • 15
  • 25
0

Try this -

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