How to get element by innerText

Anton Kandybo picture Anton Kandybo · Sep 28, 2010 · Viewed 205.1k times · Source

How to get tag in html page, if I know what text tag contains. E.g.:

<a ...>SearchingText</a>

Answer

carlin.scott picture carlin.scott · Mar 26, 2015

You could use xpath to accomplish this

var xpath = "//a[text()='SearchingText']";
var matchingElement = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

You can also search of an element containing some text using this xpath:

var xpath = "//a[contains(text(),'Searching')]";