Using the getText() Method
const element = await $('YOUR_LOCATOR'); const text = await element.getText(); console.log(text);
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.
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.
getText() method is a straightforward and commonly used approach, there are alternative methods available in WebDriverIO and other tools for extracting text from WebElements.