相关疑难解决方法(0)

puppeteer page.evaluate querySelectorAll返回空对象

我正在尝试木偶操作,这是一个示例代码,您可以在https://try-puppeteer.appspot.com/上运行它

问题是这段代码返回一个空对象数组

[{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{ },{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}, {},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{} {},{},{},{},{},{},{}]

我有什么不对吗?

const browser = await puppeteer.launch();

const page = await browser.newPage();
await page.goto('https://reddit.com/');

let list = await page.evaluate(() => {
            return Promise.resolve(Array.from(document.querySelectorAll('.title')));
        });
console.log(JSON.stringify(list))

await browser.close();
Run Code Online (Sandbox Code Playgroud)

javascript node.js puppeteer

10
推荐指数
3
解决办法
1万
查看次数

Puppeteer:Element.hover() 不存在

我正在使用 puppeteer 从网站上抓取一些图像以及一些其他数据。要更改图像,我需要将鼠标悬停在列表项上。我不断遇到有关 .hover() 的文档,但没有成功。但是, .click() 非常适合我刮擦的另一部分。

const pptr = require('puppeteer');

async function scrapeProduct(productID) {
    const browser = await pptr.launch();
    const page = await browser.newPage();
    await page.goto(`https://someplace.com`);
    let scrapeData = await page.evaluate(async () => {
        let productMap = [];

        //scrape other data...

        const imageItems = document.querySelectorAll('ul[class="images-view-list"] > li > div');
        for (let image of imageItems) {
            await image.hover();
            productMap.push({
                'Image Src': document.querySelector('div[class="image-view-magnifier-wrap"] > img').getAttribute('src'),
            });
        }
        return productMap;
    });
    await browser.close();
    return scrapeData;
}
Run Code Online (Sandbox Code Playgroud)

我已经看到您通过先执行悬停来评估页面的解决方案。这很不方便,因为我收集了许多其他数据点,并希望在一个评估请求中保持我的解决方案干净。我对 .hover() 的理解不正确吗?

node.js puppeteer

2
推荐指数
1
解决办法
1140
查看次数

Puppeteer page.evaluate 未正确返回

我正在尝试使用以下代码从网页获取所有按钮元素:

const test = await page.evaluate(() => {
        return document.querySelectorAll("button")
})

console.log(test)
Run Code Online (Sandbox Code Playgroud)

当我记录测试变量时,它会生成“未定义”,而当我在 chrome 控制台上运行以下代码时:

document.querySelectorAll("button")
Run Code Online (Sandbox Code Playgroud)

它产生正确数量的元素。

javascript puppeteer

2
推荐指数
1
解决办法
2341
查看次数

标签 统计

puppeteer ×3

javascript ×2

node.js ×2