Puppeteer sharp - how execute javascript function and return result

3k views Asked by At

I'm trying to do several tests with Puppeteer sharp but it's the first time that I work with it and I don't have very clear the syntax.

I have to open a page that I have on a server, and then run a javascript function (loadPageData('4', true)) that I have defined which can take about 15 minutes (on that page). When the function finishes, it fills a div tag on that page. I need that until the javascript function is executed and finished it does not pass to another instruction, but I do not know the syntax to use. Could you help me? what do I have to put in order to get the content of the div resultDIV?

Part of my code:

using (var browser = await Puppeteer.LaunchAsync(options))
using (var page = await browser.NewPageAsync())
{
    await page.GoToAsync("http://.../mipagina.aspx");
    page.DefaultNavigationTimeout = 100000;
    try
    {
    string funcionEjecutar = "loadPageData('4', true)";
    var res = await page.EvaluateExpressionAsync(funcionEjecutar);
    //works but no wait to finish execution of the function
    }
    catch(){
    }
}
1

There are 1 answers

0
hardkoded On

If EvaluateExpressionAsync resolves it means that the loadPageData returned some kind of value.
You could try using some Wait action after that.
For instance, WaitForSelector in there is some selector that would tell you that the process finished. You could also use WaitForFunction passing a javascript function. In that case, the task will resolve when that function returns a truthy value.