This will give all elements containing foobar text
driver.findElement(By.xpath("//*[text()[contains(.,'foobar')]]"));
If you want an exact match,
driver.findElement(By.xpath("//*[text() = 'foobar']"));
Or you can execute javascript using jQuery in Selenium
This will return all web elements containing text from parent to last child, so I use the jQuery :last selector to get the innermost node containing this text, but this may not always be accurate if you have multiple nodes containing one and the same text.
(WebElement)((JavascriptExecutor)driver).executeScript("return $(\":contains('foobar'):last\").get(0);");
If you need an exact match with the above, you need to run a filter by the results,
(WebElement)((JavascriptExecutor)driver).executeScript("return $(\":contains('foobar')\").filter(function() {" + "return $(this).text().trim() === 'foobar'}).get(0);");
jQuery returns an array of elements, if there is only one web element on the page with this specific text, you will get an array of one element. I am doing .get(0) to get this first element of an array and pass it to WebElement
Hope this helps.
LINGS source share