Getting text from WebElements with WebDriverIO(JavaScript) (using the getAttribute())
When performing web automation tasks, there are various scenarios where you may need to extract text from WebElements. WebDriverIO (WDIO) provides several methods to achieve this, and in this article, we will explore how to get text from WebElements using the `getAttribute()` method in WebDriverIO and JavaScript.
Using the getAttribute() Method
const element = await $('YOUR_LOCATOR'); const text = await element.getAttribute('textContent'); console.log(text);
The `getAttribute()` method in WebDriverIO allows us to retrieve the value of a specified attribute from a WebElement. In this case, we can use it to extract the text content of the element by specifying the 'textContent' attribute.
Here is an example test that uses the above code snippet.
The `getAttribute()` method is particularly useful when you need to retrieve text that is not directly available through the `getText()` method or when you want to extract other attributes of an element, such as 'value' or 'href'.
By utilizing the `getAttribute()` method, you can extract the desired text from WebElements and perform various assertions or actions based on the extracted content.
Leave a reply
Your email address will not be published. Required fields are marked*