简体   繁体   中英

page.EvaluateFunctionAsync() in Puppeteer Sharp is returning an empty array

I'm scraping this website for information,using Puppeteer Sharp in C#:

https://www.grimtools.com/calc/Q2z07a9Z

If I open up dev tools in Chrome, go to the console and type in "dumpSkills()", I get a nice array of JSON as a response:

(24) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]

I want to get these objects in a variable in C#.

I can get this working in JS Puppeteer using the following:

const skills = await page.evaluate(() => {
  return dumpSkills();
});

Doing so seems to return the same array. Yay, However. I've been trying to get this working in Puppeteer Sharp: These are the things that I've tried:

var skills = await page.EvaluateFunctionAsync("dumpSkills()");

var skills = await page.EvaluateExpressionAsync(@"()=>{
    return dumpDevotion();
}");

I've also tried WaitForExpressionAsync() and WaitForFunctionAsync() , but can't seem to quite get it working. I seem to get empty '[]' or '{}' depending on which method I use.

What am I doing wrong?

I created an small class model to map the values return by devotion:

class Devotion
{
  public string id { get; set; }
  public string name { get; set; }
  public string details { get; set; }
}

Then you can tell EvaluateFunctionAsync to return a list of devotions.

var devotions = await page.EvaluateFunctionAsync<List<Devotion>>("() => dumpDevotion()");

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM