Skip to main content

getHTML

Overview

Use the getHTML command to retrieve the HTML source code of a specified DOM element.

Usage

await browser.$(selector).getHTML(includeSelectorTag);

Command Parameters

NameTypeDescription
includeSelectorTagBooleanOptional parameter. Include the selector element's tag in the source code. Default: true.

Usage Examples

index.html

<div id="test">
<span>Lorem ipsum dolor amet</span>
</div>

getHTML.js

it("should get html for certain elements", async ({ browser }) => {
var outerHTML = await browser.$("#test").getHTML();
console.log(outerHTML);
// outputs:
// "<div id="test"><span>Lorem ipsum dolor amet</span></div>"

var innerHTML = await browser.$("#test").getHTML(false);
console.log(innerHTML);
// outputs:
// "<span>Lorem ipsum dolor amet</span>"
});

References

We'd like to give credit to the original WebdriverIO docs article, from which we drew some information while writing our version.