'RobotFramework: Finding Xpath containing text

So my link at the below location contains the words 'Download certificate'

xpath=//*[@id="training_list_table"]/tbody/tr/td[5]/span/a/text()

I want to be more specific in my search so that it doesn't just accept any old text so something like...

xpath=//*[@id="training_list_table"]/tbody/tr/td[5]/span/a/text()='Download certificate'

But this lets me down. What is the correct syntax to do such a search?



Solution 1:[1]

If you want to match link that

  • contains text "Download certificate" you might use

    //a[contains(., "Download certificate")]
    
  • starts with text "Download certificate":

    //a[starts-with(., "Download certificate")]
    
  • has exact text "Download certificate":

    //a[.="Download certificate"]
    

If there are more than one link that contains "Download certificate" in a table you might specify appropriate ancestor by specific attribute, text or index, e.g. //td[5]//a[.="Download certificate"]- you don't have to include group of ancestors in your XPath like .../tbody/tr/td[5]/span/...

Solution 2:[2]

If I understand correctly, you could try something like this:

xpath=//*[@id="training_list_table"]/tbody/tr/td[5]/span/a[contains(text(), 'Download certificate')]

//*[contains(text(), '')] is the key..

Solution 3:[3]

Ok, try this:

xpath=//*[@id="training_list_table"]/tbody/tr/td[5]/span/a[@text='Download certificate']

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 user8166432
Solution 3 Tyler2P