How to wait for a javascript expression to be true
Contributors: Darío Kondratiuk
Problem
Section titled “Problem”WaitForSelectorAsync is not enough, you want to wait for a more complex javascript expression to be truthly.
Solution
Section titled “Solution”Use Page.WaitForExpressionAsync or Page.WaitForFunctionAsync to delay execution until the result of a javascription expression is truthly.
If it’s a simple expression you can use WaitForFunctionAsync:
using (var browser = await Puppeteer.LaunchAsync(options))using (var page = await browser.NewPageAsync()){ await page.GoToAsync("https://www.somepage.com"); await Page.WaitForExpressionAsync("document.queryselector('#status_info').innerText.match('^Showing ([1-9][0-9]*?) to ([1-9][0-9]*?)') of ([1-9][0-9]*?) entries') != null");}If the evaluation is more complex, you could wrap it inside a function and use WaitForFunctionAsync:
var waitTask = Page.WaitForFunctionAsync(@"() =>{ return document.queryselector('#status_info').innerText.match('^Showing ([1-9][0-9]*?) to ([1-9][0-9]*?)') of ([1-9][0-9]*?) entries') != null;}");Made with love by Dario Kondratiuk