I’m trying to retrieve the innerHTML of elements using JavaScript, especially when Selenium WebDriver (Java) can’t locate them directly. I want to achieve something like:
getElementByXpath("//html[1]/body[1]/div[1]").innerHTML
Since not all elements have an ID, I need a reliable JavaScript get element by XPath approach that I can execute within Selenium. What’s the best way to do this?
I’ve been doing Selenium for a while, and yeah, Is there a way to use JavaScript to get an element by XPath in Selenium WebDriver?
Absolutely! When normal locators don’t cut it, you can execute JavaScript directly to find elements by XPath. For example, inside JavascriptExecutor, you can run:
function getElementByXpath(path) {
return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
Then in Java, you might do:
JavascriptExecutor js = (JavascriptExecutor) driver;
String script = "return document.evaluate(\"//html/body/div[1]\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.innerHTML;";
String innerHtml = (String) js.executeScript(script);
Works like a charm when normal Selenium locators fail.
Building on that, yes, It’s definitely doable, but a heads-up—XPath can be fragile if your page structure changes often. So while the JS snippet below is handy:
document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue
you’d wrap that inside Selenium’s executeScript()
to grab your element. But if you can, try to use more stable locators like data-testid
or ARIA attributes, especially if you have some control over the application’s codebase. Keeps your tests less flaky.
From experience, Yeah, absolutely—especially when the usual By.xpath()
in WebDriver just refuses to find your element
. Just do this in your Java code:
String innerHTML = (String) ((JavascriptExecutor) driver).executeScript(
"return document.evaluate(\"" + xpath + "\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.innerHTML;"
);
It’s honestly a lifesaver for those tricky cases when all else fails. Just keep in mind, relying too much on XPath via JS might make your tests brittle over time, so use it wisely!