【问题标题】:Puppeteer can't find selectorPuppeteer 找不到选择器
【发布时间】:2018-05-29 16:23:03
【问题描述】:

我正在尝试使用 Puppeteer 进行一些网络抓取,但脚本似乎无法找到我正在寻找的选择器。基本上这段代码:

const puppeteer = require('puppeteer');

let scrape = async () => {
const year = 18;

const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.goto('https://cobbcounty.org/index.php?option=com_wrapper&view=wrapper&Itemid=2008');
await page.waitFor(5000);
var id = '';
for(i=0;i<10000;i++){
    id = i;
    await page.click('#txtCase');
    await page.keyboard.type(year + '-P-' + id);
    await page.select('#lstDoc','Estate');
}
}

scrape().then((value) => {
console.log('script ended');
});

给我这个错误:

(node:31125) UnhandledPromiseRejectionWarning: AssertionError 
[ERR_ASSERTION]: No node found for selector: #txtCase

据我所知,#txtCase 是页面上的实际选择器,所以我不知道为什么 puppeteer 找不到它。如果有人可以向我解释我做错了什么,那将非常有帮助。

【问题讨论】:

    标签: node.js chromium puppeteer


    【解决方案1】:

    据我所知,#txtCase 是页面上的实际选择器,所以我不知道为什么 puppeteer 找不到。

    尝试加载页面并使用控制台查找该元素。

    document.querySelector('#txtCase')
    null
    

    它不在那里。我知道当您右键单击以检查该文本字段时可以看到它,但它嵌套在 iframe 中。您需要访问该框架,然后找到该按钮,然后单击它。

    const frame = await page.frames().find(f => f.name() === 'iframe');
    const button = await frame.$('#txtCase');
    button.click();
    

    【讨论】:

      【解决方案2】:

      如果你只是想让它工作,你可以使用一种奇怪的方式来实现它:

      await page.mouse.click( coordinate x, coordinate y { button: 'left' })
      

      所以你不需要选择器,只需要坐标。

      【讨论】:

        猜你喜欢
        • 2021-11-25
        • 2021-03-23
        • 1970-01-01
        • 1970-01-01
        • 2022-06-16
        • 2020-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多