Puppeteer 找不到选择器
Puppeteer can't find selector
我正在尝试使用 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 找不到它。如果有人可以向我解释我做错了什么,那将非常有帮助。
As far as I can tell, #txtCase is an actual selector on the page, so I don't know why puppeteer can't find it.
尝试加载页面并使用控制台查找该元素。
document.querySelector('#txtCase')
null
它不在那里。我知道您可以在右键单击以检查该文本字段时看到它,但它嵌套在 iframe 中。您需要访问该框架,然后找到按钮,然后单击它。
const frame = await page.frames().find(f => f.name() === 'iframe');
const button = await frame.$('#txtCase');
button.click();
如果你只是想让它工作,你可以用一种奇怪的方式使用:
await page.mouse.click( coordinate x, coordinate y { button: 'left' })
所以你不需要选择器,只需要坐标。
我正在尝试使用 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 找不到它。如果有人可以向我解释我做错了什么,那将非常有帮助。
As far as I can tell, #txtCase is an actual selector on the page, so I don't know why puppeteer can't find it.
尝试加载页面并使用控制台查找该元素。
document.querySelector('#txtCase')
null
它不在那里。我知道您可以在右键单击以检查该文本字段时看到它,但它嵌套在 iframe 中。您需要访问该框架,然后找到按钮,然后单击它。
const frame = await page.frames().find(f => f.name() === 'iframe');
const button = await frame.$('#txtCase');
button.click();
如果你只是想让它工作,你可以用一种奇怪的方式使用:
await page.mouse.click( coordinate x, coordinate y { button: 'left' })
所以你不需要选择器,只需要坐标。