Getting text from WebElements with WebDriver IO (Javascript)

Getting text from WebElements with WebDriver IO (Javascript)

When performing web automation tasks, it is often necessary to extract text from WebElements. Whether you need to verify the text of a specific element or retrieve data for further processing, WebDriverIO (WDIO) provides convenient methods to accomplish this. In this article, we will explore how to get text from WebElements using WebDriverIO and JavaScript.

Using the getText() Method

const element = await $('YOUR_LOCATOR');
const text = await element.getText();

The getText() method in WebDriverIO allows you to extract the visible text from a WebElement. It returns the combined text of all immediate child nodes of the element, excluding the text from its descendants.

Here is an example test that uses the above code snippet.

See Working Example

The getText() method is especially useful when you need to validate the text content of specific elements on a web page. It enables you to retrieve the visible text and perform assertions or further actions based on the extracted content.

By utilizing the getText() method, you can easily extract the desired text from WebElements and perform various actions based on the extracted content.

Alternative Approaches

Although the getText() method is a straightforward and commonly used approach, there are alternative methods available in WebDriverIO and other tools for extracting text from WebElements.

Leave a reply

Your email address will not be published. Required fields are marked*

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.