Selenium - XPATH - Searching for element by innerHTML Selenium - XPATH - Searching for element by innerHTML selenium selenium

Selenium - XPATH - Searching for element by innerHTML


You can't use XPath to match by inner HTML, but you can use it to match by 'inner text' :

//*[text()[contains(., 'someUniqueString')]]

`demo

The above XPath should return the code element since it is parent element of the target text 'someUniqueString'.


Try the following XPath:

//*[contains(text(),'someUniqueString')]

Note: As the code element set with display: none, the element is NOT visible, though you can find the element. If you try to interact with the element using click or other API, you might get ElementNotVisisbleException. To put it simply, You can't interact with the elements which are NOT displayed on GUI (browser).


If you are NOT able to find the element, then there are good chances that your element is inside an iframe. In such cases, you must switch to the frame first and then use the XPath to find the element.

More details on switching between frames here.