Search code examples
webdriver-io

Webdriver.io browser.getText() sometimes returns undefined


I have this piece of code:

getText: (selector) => {
    browser.waitUntil(function () {
        return browser.isExisting(selector) === true;
    },timeout,
        'Could not find element after: ' + timeout,
        pollingTime);

    return browser.getText(selector);
}

And sometimes this function (getText(selector), but in deep browser.getText(selector)) returns undefined for the selector that looks like this:

article[data-product-id="test-00020"] li.product-entry__summary__item.is-price span

This doesn't happen every time test is run, but it happens occasionally. It is driving me nuts because the behavior is inconsistent. Sometimes it works and sometimes it doesn't.
Did anybody have similar problems? Please help! Thank you.


Solution

  • getText is dependent on the element being visible in the viewport of the page (so if it's scrolled off the page it will return an empty string)

    Instead, you can use getHTML(false) to get the text content of an element (just ensure it's the inner-most element, otherwise you'll get HTML elements in the returned content)