Wait for text to appear when using Puppeteer
You can use waitForFunction
. See https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagewaitforfunctionpagefunction-options-args
Including @elena's solution for completeness of the answer:
await page.waitForFunction('document.querySelector(".count").innerText.length == 7');
Apart from the method presented in the answer from nilobarp, there are two more ways to do this:
page.waitForSelector
Using the pseudo selector :empty
it is possible to find elements that contain no child nodes or text. Combining this with the :not
selector, we can use page.waitForSelector
to query for a selector which is not empty:
await page.waitForSelector('.count:not(:empty)');
XPath expression
If you not only want to make sure that the element is not empty, but also want to check for the text it contains, you can use an XPath expression using page.waitForXPath
:
await page.waitForXPath("//*[@class='count' and contains(., 'Expected text')]");
This line will only resolve after there is an element on the page which has the attribute class="count"
and contains the text Expected text
.