首页 > 其他分享 >How to fix waitForFunction TimeoutError of puppeteer All In One

How to fix waitForFunction TimeoutError of puppeteer All In One

时间:2023-09-05 09:44:32浏览次数:32  
标签:selector const fix await puppeteer How waitForFunction page

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));

image

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);
})();

(

标签:selector,const,fix,await,puppeteer,How,waitForFunction,page
From: https://www.cnblogs.com/xgqfrms/p/17678513.html

相关文章

  • I Asked ChatGPT How to Become a Millionaire as a Developer
    MyfriendJoshandIfinishedourcollagetogether.Eventhoughweworkedatdifferentplaces,ourincomeanddesignationweremoreorlesssimilar.Lastyearhebecameamillionaireattheageof29!Letmestartbyaskingyouthisquestion:Isbecomin......
  • How to automatically run a scheduled task every hour in Node.js All In One
    HowtoautomaticallyrunascheduledtaskeveryhourinNode.jsAllInOne如何在Node.js中每间隔一小时自动运行一个定时任务引用场景Node.js后台爬虫服务,定时爬去指定页面,抓取最新数据,并写入到数据库中;在同一个Node.js部署环境中,没有使用Linux的crontab权......
  • ShowMeBug X 得物APP | 精准识别技术人才,高效搭建潮流电商团队
    ShowMeBug签约了新一代潮流网购社区——得物APP,并在技术的赋能下,为得物APP提供了有效的技术人才招聘解决方案,帮助企业快速识别优质技术人才,精准评估技术人才实力。得物APP通过ShowMeBug 技术测评环节的智能组卷和自动评卷功能,HR高效完成对技术候选人的初步筛选,为技术面试官节......
  • 字节青训营 x ShowMeBug:项目实战,弥补学用鸿沟
    近期,ShowMeBug与字节青训营联合举办针对技术人群的培训实践活动,该活动目前正如火如荼地进行中,并即将迎来尾声。  这是一次理念相契的合作。字节青训营是稀土掘金社区发起的技术培训和人才选拔项目,面向在校大学生,旨在培养优秀且具有职业竞争力的开发工程师。在传统的教育视......
  • how to use R2023a_Doc_Windows.iso
    referencepage:https://www.mathworks.com/help/install/ug/install-documentation-on-offline-machines.htmlmpminstall-doc--matlabroot="C:\ProgramFiles\MATLAB\R2023a"......
  • Linux修改挂载的移动硬盘目录权限失败问题原因(chown,chmod不起效)
    场景:移动硬盘插到刷Linux系统的机顶盒上当作共享存储,开webdav需要将文件改为普通用户所有和data-www组原因:移动硬盘是Windows系统的ntfs格式,它的权限只能在挂载时进行指定,而无法使用Linux命令修改,如果有精细权限控制最好还是转为Linux的分区格式解决方法:修改/etc/f......
  • Apple script to show or hide window
    https://apple.stackexchange.com/questions/98123/what-is-the-applescript-command-to-show-or-hide-iterm2-windowHereareseveraloptions(noneofthemhavebeentested,sotestandseewhichoneworksforyou):Option1:tellapplication"iTerm2"......
  • CTFSHOW_你会异或吗
    Python文件操作python安装(vscode)在VSCode中选择Python解释器的方法vscodesettings.json设置file=open("misc5.png","rb")content=file.read()withopen("flag.png","wb")asa:forcontentsincontent:a.write(bytes([c......
  • CTFSHOW_web1
    PHPintval()函数wp1wp2intval()函数和mysqllimit用法......
  • CTFshow misc11-20
    misc11:没有头绪,看了网上大佬的一些思路,受益匪浅,和大家分享下。1.解压zip文件,用winhex打开misc11.png2.判断文件格式是否篡改,检查png的文件头和文件尾,文件格式正常PNG文件头(hex):89504e470d0a1a0aPNG文件尾(hex):0000000049454E44AE4260823.判断否有文件......