node.js - 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');
有什么建议吗?
解决方案
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())
})()
推荐阅读
- jenkins - 更改 Jenkins 参数变量值
- javascript - 如何让我的 react 应用程序的一部分仅对订阅者开放?
- python - 在多行上打印一个字符串
- xcode - 安装 react-native-webrtc 后应用程序未构建
- docker - 在 AML docker 上运行 Pytorch 时,纪元时间增加
- python - 如何将天数(浮动)添加到熊猫时间戳?
- google-cloud-dataflow - 谷歌数据流 PCollection 加入
- c# - 从 SQLite 数据库读取表总是返回 null
- javascript - 为什么我收到错误“Uncaught TypeError: Cannot read property 'classList' of null”?
- go - 基于“广播”或“房间”的 Websocket 策略