How to fix waitForFunction TimeoutError of puppeteer All In One
page.waitForFunction
超时错误
error ❌
TimeoutError: Waiting failed: 30000ms exceeded
const selector = '.foo';
await page.waitForFunction((selector) => !!document.querySelector(selector));
solution ✅
const selector = '.foo';
await page.waitForFunction(
selector => !!document.querySelector(selector),
{},
selector
);
demos
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2023-09-01
* @modified
*
* @description
* @description
* @difficulty Medium
* @time_complexity O(n)
* @space_complexity O(n)
* @augments
* @example
* @link https://www.cnblogs.com/xgqfrms/p/17678513.html
* @link https://www.cnblogs.com/xgqfrms/p/17673122.html
* @solutions
*
* @best_solutions
*
*/
const log = console.log;
// import puppeteer from 'puppeteer';
const puppeteer = require('puppeteer');
class Crawler {
constructor(url = '') {
this.url = url;
}
async test (selector = '') {
const browser = await puppeteer.launch({headless: "new"});
const page = await browser.newPage();
await page.goto(this.url);
await page.setViewport({width: 1920, height: 1080});
const matches = await page.waitForFunction((selector) => {
console.log(`❓ selector`, selector)
// ❌ passed selector
const arr = [...document.querySelectorAll(selector)];
// ✅ hard code string selector
// const arr = [...document.querySelectorAll('.trim-title-container')];
return arr.length ? arr : null;
});
console.log(`✅ matches`, matches)
const contents = await matches.evaluate(els => els.map(e => e.innerText));
console.log(`✅ contents`, contents)
await browser.close();
}
}
(async () => {
const url = `https://www.tesla.cn/modely/design#overview`;
const selector = `.trim-title-container`;
const crawler = new Crawler(url)
await crawler.test(selector);
})();