gpt4 book ai didi

javascript - 停止 puppeteer 进程,直到全局 var 值从 false 变为某个字符串

转载 作者:行者123 更新时间:2023-11-30 20:24:53 24 4
gpt4 key购买 nike

我在我的 nodejs 服务器中运行 puppeteer ...想法是打开一个页面,通过 socket.io 读取验证码并将其发送给用户,当从用户接收到输入的值验证码以继续使用 puppeteer ...这里被简化了我的代码版本

   var captcha = false ;

io.on('connection' , function(socket){

socket.on('start_puppeteer' , function (data) {
io.emit('push_msg' , {text : 'starting puppeteer .... ' });
run(io);
})

socket.on('get_captcha_from_client' , function (data) {

captcha = data.text ;
console.log( 'capthca set : ' + captcha);
})

});

function get_captcha() {
return new Promise(function(resolve , reject ) {
if(captcha === false )
reject(captcha );
else
resolve(captcha);
});
}


async function run(io) {
const browser = await puppeteer.launch();
const page = await browser.newPage();

await page.goto('http://localhost');

console.log( ' sending captcha to client ' );

io.emit('send_captcha_to_client' , {text : 'captcha text' });

var captcha = await get_captcha();

console.log( ' captcha confirm -> ' + captcha );

browser.close();
}

问题是即使设置了验证码,代码也不会移动,我也没有在控制台中得到 captcha confirm

$ node pop.js
sending captcha to client
(node:9356) UnhandledPromiseRejectionWarning: false
(node:9356) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().
(rejection id: 3)
(node:9356) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
capthca set : 1234

最佳答案

page.waitForFunction 计算一个函数 in browser context ,即您可以检查某个 DOM 节点是否存在或输入的值,但您只能访问该函数内部的页面对象。

现在,如果您只想等待验证码被解决,让您的 get_captcha 返回 Promise 并为它的结果 await :

function get_captcha() {
return new Promise(resolve => {

// ...Do something lengthy here...

resolve(captcha);
});
}

async function run(io) {
// ....

await page.goto('http://localhost');
io.emit('send_captcha_to_client' , {text : 'captcha text' });
var captcha = await get_captcha();

// ....
}

关于javascript - 停止 puppeteer 进程,直到全局 var 值从 false 变为某个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51028799/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com