PuppeteerSharp 的 Puppeteer 函数

Puppeteer Function to PuppeteerSharp

我对 puppeteersharp 和一般的 puppeteer 是新手。 我正在尝试将 puppeteer 中使用的函数转换为 puppeteerSharp,我想知道该怎么做。 这是木偶操作函数(这个向下滚动到页面末尾):

async function autoScroll(page){
await page.evaluate(async () => {
    await new Promise((resolve, reject) => {
        var totalHeight = 0;
        var distance = 100;
        var timer = setInterval(() => {
            var scrollHeight = document.body.scrollHeight;
            window.scrollBy(0, distance);
            totalHeight += distance;

            if(totalHeight >= scrollHeight){
                clearInterval(timer);
                resolve();
            }
        }, 100);
    });
});

任何人都可以告诉我如何设法让它与 puppeteerSharp 一起工作吗? 亲切的问候

您可以调用 EvaluateFunctionAsync 并将该函数作为 string 传递。

await page.EvaluateFunctionAsync(@"async () => {
    await new Promise((resolve, reject) => {
        var totalHeight = 0;
        var distance = 100;
        var timer = setInterval(() => {
            var scrollHeight = document.body.scrollHeight;
            window.scrollBy(0, distance);
            totalHeight += distance;

            if(totalHeight >= scrollHeight){
                clearInterval(timer);
                resolve();
            }
        }, 100);
    });
}");