首页 > 解决方案 > Puppeteer 防止“暂停调试”也就是禁用所有断点

问题描述

我需要用无头模式抓取一个有很多debugger;

有没有办法防止调试器暂停?

我尝试使用此代码发送加载 CTRL+F8 和 F8 但没有成功!

await crt_page.keyboard.down('Control');
await crt_page.keyboard.press('F8');
await crt_page.keyboard.up('Control');
await crt_page.keyboard.press('F8');

有什么建议吗?

标签: node.jsgoogle-chromepuppeteer

解决方案


Puppeteer 会自动在 中按下键page,而不是在浏览器中。

所以我认为解决方案是安装一个 npm 包robotjs来在浏览器之外做事。

希望这对你有帮助!

如果此代码有效,请不要忘记选择我的答案作为正确答案。

const puppeteer = require('puppeteer')
const robot = require('robotjs')

;(async () => {
    const browser = await puppeteer.launch({
        headless: false,
        devtools: true
    })
    const [page] = await browser.pages()

    const open = await page.goto('https://www.example.com', { waitUntil: 'networkidle0', timeout: 0 })

    await page.waitFor(4000)
    await robot.keyToggle(']','down','control') // For Mac, change 'control' to 'command'

    await page.waitFor(500)
    await robot.keyToggle(']','down','control') // For Mac, change 'control' to 'command'

    await page.waitFor(500)
    await robot.keyToggle(']', 'up', 'control') // For Mac, change 'control' to 'command'

    await page.waitFor(1000)
    await robot.keyToggle('f8','down','control') // For Mac, change 'control' to 'command'

    await page.waitFor(500)
    await robot.keyToggle('f8', 'up', 'control') // For Mac, change 'control' to 'command'


})()

要调试您的robotjs,它是否有效,请尝试此代码。

下面的代码运行 puppeteer 并使用robotjs.

如果这在您的服务器上也不起作用,那么对不起,我无法帮助您。

const puppeteer = require('puppeteer')
const robot = require('robotjs')
const pageURL = 'https://www.google.com'
const normal_Strings = ['`','1','2','3','4','5','6','7','8','9','0','-','=','[',']','\\',';','\'',',','.','/']
const shiftedStrings = ['~','!','@','#','$','%','^','&','*','(',')','_','+','{','}','|',':','"','<','>','?']

;(async () => {
    const browser = await puppeteer.launch({
        headless: false,
        devtools: true
    })
    const [page] = await browser.pages()

    const open = await page.goto('https://www.example.com', { waitUntil: 'networkidle0', timeout: 0 })

    console.log('First URL:')
    console.log(await page.url())
    await robot.keyToggle('l','down','control') // For Mac, change 'control' to 'command'
    await page.waitFor(500)
    await robot.keyToggle('l', 'up', 'control') // For Mac, change 'control' to 'command'
    await page.waitFor(1000)
    for (let num in pageURL) {
        if (shiftedStrings.includes(pageURL[num])) {
            var key = normal_Strings[ shiftedStrings.indexOf(pageURL[num]) ]

            await robot.keyToggle( key,'down','shift')
            await page.waitFor(300)
            await robot.keyToggle( key, 'up', 'shift')
            await page.waitFor(300)
        }
        await robot.keyTap(pageURL[num])
        await page.waitFor(200)
    }
    await page.waitFor(1000)
    await robot.keyTap('enter')

    await page.waitForSelector('img#hplogo[alt="Google"]', {timeout: 0})
    console.log('Second URL:')
    console.log(await page.url())
})()

推荐阅读