Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

7
  • 8
    Thank you for the reply, it was 50% of what I needed (got me started). The form I arrived to is this "(//*[contains(text(), '" + text + "')] | //*[@value='" + text + "'])" it will search for given text not only inside element nodes, but also inside input elements whose text was set via 'value' attribute i.e. <button value="My Button" /> . Though do note, the value must be strict match, not just contain the text. Commented Oct 11, 2014 at 20:37
  • 17
    Also worth mentioning for other search engine visitors: if you're looking for a link, there are find_element(s)_by_link_text and find_element(s)_by_partial_link_text methods Commented Nov 17, 2014 at 23:45
  • 4
    What if the text is dynamic? That is, might contain quotes. Wouldn't that break this solution? Commented Jul 8, 2015 at 17:59
  • 4
    Searching for certain names seems to break this. Take the following for an example: "//*[contains(text(), '"+username+"')]" if username = "O'Reilly"; then the xpath would become invalid. Is there a way around this? Commented Jun 7, 2016 at 19:37
  • 3
    It doesn't seem to work when the target text has multiple lines. Commented Nov 20, 2016 at 18:25