How to select a web element by text with Selenium WebDriver, Java How to select a web element by text with Selenium WebDriver, Java selenium selenium

How to select a web element by text with Selenium WebDriver, Java


Css does not allow you do text based search. xpath is the only option there.

//div[contains(text(),'noreply@somedomain.com')]


contains(node, substring) might solve the problem, but note that if there are several elements with similar text contents, e.g.

  • noreply@somedomain.com

  • office.noreply@somedomain.com

  • home.noreply@somedomain.com

Predicate [contains(text(),'noreply@somedomain.com')] will match all of them

In this case it's better to use starts-with(node, substring):

//div[starts-with(text(),'noreply@somedomain.com')]

or fetch required element by exact text content:

//div[text()='noreply@somedomain.com']