我正在尝试使用 puppeteer 来检查网页上是否存在类。例如,假设您想抓取某些数据,并且您知道这些数据存储在某个类中。要获取数据,您需要使用类名来获取数据。这是我尝试使用的代码。这是行不通的。
let pageClicked = document.querySelector('.classIAmTryingToFind')
if(pageClicked){
console.log('False')
await browser.close()
}else{
console.log('True')
await browser.close()
}
Run Code Online (Sandbox Code Playgroud)
当我运行代码时出现此错误。
UnhandledPromiseRejectionWarning: Error: Protocol error (Runtime.callFunctionOn): Target closed.
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Puppeteer 自动执行查询该网站上的数据的任务。因此,我需要选择数据集(每日摘要,第一个选项),然后选择位置类型(州,第三个选项),然后选择州(阿拉斯加,第二个选项)。问题是我的代码没有更改为下一个表。因此,在选择数据集中的第一个选项(每日摘要)后,它不会选择第三个选项(州),而是再次选择数据集表中的第三个选项!我是 Puppeteer 的新手,所以我真的不知道该怎么做。任何帮助表示赞赏。
下面是我的代码:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({headless:false})
const page = await browser.newPage()
const navigationPromise = page.waitForNavigation()
await page.goto('https://www.ncdc.noaa.gov/cdo-web/datatools/selectlocation')
await page.waitForSelector('.selectLocationFilters > .datasetContainer > .slideElement > #datasetSelect > option:nth-child(1)')
await page.click('.selectLocationFilters > .datasetContainer > .slideElement > #datasetSelect > option:nth-child(1)')
await page.select('.inset #locationCategorySelect', '')
await page.waitForSelector('.selectLocationFilters > .locationCategoryContainer > .locationCategoryFilter > #locationCategorySelect > option:nth-child(3)')
await page.click('.selectLocationFilters > .locationCategoryContainer > .locationCategoryFilter > #locationCategorySelect > option:nth-child(3)')
await page.select('.inset #selectedState', '')
await …Run Code Online (Sandbox Code Playgroud)我正在尝试使用 Node.js 通过 Puppeteer / Headless Chrome 从我们的 Intranet 中的页面生成 .pdf。
从随机页面生成 .pdf 没有问题,但我在打开页面时出现的登录框架上遇到了困难。
它看起来像这样:
我找不到访问“Nutzername”和“Passwort”这两个字段来登录的方法,因为 Chrome 的开发人员工具不显示字段名称。
打开此页面时生成的 .pdf 文件或屏幕截图始终显示“未经授权”,就好像 Headless Chrome 在此对话框中按下 Abbrechen(中止)一样。
到目前为止我尝试过的是这样的:
const puppeteer = require('puppeteer');
(async () => {
let url = 'http://intranet...../'
let browser = await puppeteer.launch()
let page = await browser.newPage()
await page.goto(url, {waitUntil: 'networkidle2' })
await page.type('#Nutzername', '....'); // Error here, because the identifier is wrong.
await page.keyboard.press("Tab");
await page.type('#Passwort', '....');
await page.type(String.fromCharCode(13)); // Enter
await page.pdf({
format:"A4",
path:'./Speiseplan.pdf',
displayHeaderFooter: false,
printBackground:true …Run Code Online (Sandbox Code Playgroud) 我正在编写一个机器人,在任务完成后我试图用它来抓取 reCAPTCHA 令牌。我正在尝试使用:
await page.evaluate(() => document.getElementById('recaptcha-token').value)
Run Code Online (Sandbox Code Playgroud)
验证码加载到页面后,但是每次我都会收到相同的错误:Uncaught (in promise) Error: Evaluation failed: TypeError: Cannot read property 'value' of null。
我认为此错误部分是由于我尝试获取的元素属于隐藏类型而引起的:
<input type="hidden" id="recaptcha-token value="[very long string of letters and numbers]">
Run Code Online (Sandbox Code Playgroud)
我该如何绕过这个?
我正在尝试使用 Puppeteer 生成 PDF 并在其中插入 Chart.js 图表(但整个 PDF 应该是基于文本的 - 而不是网页屏幕截图)。当我打开模板时,我可以看到加载的图形,但在 PDF 中只有一个空白。我像这样生成它:
async function generatePdf() {
let data = { value: "myValue" };
getTemplateHtml()
.then(async (res) => {
// Now we have the html code of our template in res object
// you can check by logging it on console
// console.log(res)
console.log("Compiing the template with handlebars");
const template = hb.compile(res, { strict: true });
// we have compile our code with handlebars
const result = template(data);
// We …Run Code Online (Sandbox Code Playgroud) 我有一个工作区,可以在其中添加不同的对象。有一种场景,双击时可以在工作区中自动添加一个对象。我尝试过不同的解决方案,但没有一个真正有效。
这是我尝试过的:
await page.evaluate(selector => {
var targLink = document.querySelector(selector);
var clickEvent = document.createEvent('MouseEvents');
clickEvent.initEvent('dblclick', true, true);
targLink.dispatchEvent(clickEvent);
}, selector)
Run Code Online (Sandbox Code Playgroud) 我正在尝试制作我的个人推特帖子抓取工具,但这似乎比我想象的要难。
这个想法是首先获取 div 帖子,然后浏览它们并最终获得帖子文本。
这并不像我想象的那样,目前我只得到一个空数组。
我究竟做错了什么?
const puppeteer = require('puppeteer');
var fs = require('fs');
async function getQuotes(){
const browser = await puppeteer.launch({headless:true});
const page = await browser.newPage();
await page.goto('https://twitter.com/Twitter', {waitUntil: 'networkidle2'}); // page url
const result = await page.$$eval('div.css-901oao.r-jwli3a.r-1qd0xha.r-a023e6.r-16dba41.r-ad9z0x.r-bcqeeo.r-bnwqim.r-qvutc0', rows => {
return rows.map(row =>{
const properties = {};
const post = row.querySelector('span.css-901oao.css-16my406.r-1qd0xha.r-ad9z0x.r-bcqeeo.r-qvutc0');
properties.text = post.innerText;
return properties;
});
});
console.log(result);
browser.close();
}
getQuotes();
Run Code Online (Sandbox Code Playgroud) Puppeteer 预装的 chromium 根本不起作用,所以我依次安装了 ubuntu 并将apt install chromium-browser可执行路径更改为usr/bin/chromium-browser
现在,puppeteer 不会抛出任何错误,但我得到了这个错误:
[935270:935270:1222/220755.094434:ERROR:browser_main_loop.cc(1434)] 无法打开 X 显示。
我尝试了论坛上的所有红色内容,但没有成功。
setversionNumber = async (index, page) => {
const input = await page.evaluateHandle((i) => {
const versionView = document.getElementsByClassName("mx-dataview mx-name-dataView9")[0];
const inputfield = versionView.getElementsByTagName('input')[i];
if (inputfield.value === null || inputfield.value === '' ) {
console.info('returning input');
return inputfield;
}
console.info('returning null');
return null;
}, index);
if (await input !== null) {
console.info('input is filled');
const content = await page.evaluate((i) => i.value, input);
console.log(content);
await input.type('1');
} else {
console.info('input is empty');
}
Run Code Online (Sandbox Code Playgroud)
我得到了这段代码,它需要获取一个输入字段,如果输入的值为空,则输入“1”。当我在浏览器中运行它时,控制台会记录“reruning null”,这是预期的,因为输入值为空。
但是,当检查返回的输入是否实际上为空时,它会记录“输入已填充”。之后会抛出错误,提示“无法读取 null 的属性“值””。这意味着输入实际上为空。所以我认为我只是没有使用正确的检查来查看这个输入对象是否实际上为空。
是否有其他方法来检查 page.evaluateHandle 返回的对象是否为空/null?
我想使用 Puppeteer 生成 PDF。
\n我的环境开发
\n我在网上尝试了很多教程来做到这一点。\n但是代码被阻止browser.newPage()
const browser = await puppeteer.launch();\nconsole.log(await browser.version()); //HeadlessChrome/90.0.4427.0 \nconst page = await browser.newPage(); //after it does nothing\nRun Code Online (Sandbox Code Playgroud)\n这里是调试(最后六行):
\n puppeteer:protocol:RECV \xe2\x97\x80 {"id":4,"result":{"sessionId":"B3D2567E07203D4A096A495710466966"}} +0ms\n puppeteer:protocol:SEND \xe2\x96\xba {"sessionId":"B3D2567E07203D4A096A495710466966","method":"Page.enable","params":{},"id":5} +1ms\n puppeteer:protocol:SEND \xe2\x96\xba {"sessionId":"B3D2567E07203D4A096A495710466966","method":"Page.getFrameTree","params":{},"id":6} +1ms\n puppeteer:protocol:SEND \xe2\x96\xba {"sessionId":"B3D2567E07203D4A096A495710466966","method":"Target.setAutoAttach","params":{"autoAttach":true,"waitForDebuggerOnStart":false,"flatten":true},"id\n":7} +0ms\n puppeteer:protocol:SEND \xe2\x96\xba {"sessionId":"B3D2567E07203D4A096A495710466966","method":"Performance.enable","params":{},"id":8} +0ms\n puppeteer:protocol:SEND \xe2\x96\xba {"sessionId":"B3D2567E07203D4A096A495710466966","method":"Log.enable","params":{},"id":9} +0ms\n\nRun Code Online (Sandbox Code Playgroud)\n谢谢,
\npuppeteer ×10
javascript ×6
node.js ×6
chart.js ×1
debian ×1
electron ×1
headless ×1
linux ×1
playwright ×1
recaptcha ×1
twitter ×1
ubuntu ×1
web-scraping ×1