Skip to main content

getText

Overview

Use the getText command to retrieve the text content from a DOM element.

Ensure that the element from which you want to request text is interactable, otherwise, the command will return an empty string. If the element is disabled or not visible and you still want to get its text content, use the getHTML command as a workaround.

Usage

await browser.$(selector).getText();

Usage Examples

index.html

<div id="elem">
Lorem ipsum <strong>dolor</strong> sit amet,<br />
consetetur sadipscing elitr
</div>
<span style="display: none">I am invisible</span>

getText.js

it("should demonstrate the getText function", async ({ browser }) => {
console.log(await browser.$("#elem").getText());
// outputs: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr"

console.log(await browser.$("span").getText());
// outputs: "" (empty string) because the element is not interactable
});

it("get content from table cell", async ({ browser }) => {
await browser.url("http://the-internet.herokuapp.com/tables");

const rows = await browser.$$("#table1 tr");
const columns = await rows[1].$$("td"); // get columns of the 2nd row

console.log(await columns[2].getText()); // get text from the 3rd column
});

References

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