我正在使用 puppeteer 和 https://www.deathbycaptcha.com/自动解决验证码。
这是我解决验证码的函数。
async function solve_captcha(page, client, captcha_selector, imgName) {
console.log("0")
const attr = await page.$$eval(captcha_selector, el => el.map(x => x.getAttribute("style")));
console.log("1")
const captchaUrl = attr[0].match(/(?<=url\(')(.*)(?='\))/g)[0];
console.log("2")
await base64ImageDecoder.convert(captchaUrl, './', imgName).then(result => {console.log(result);}).catch(err => console.error(err));
console.log("3")
const captcha_file = imgName+'.jpg';
console.log("4" + captcha_file)
await client.get_balance((balance) => {
console.log(balance);
});
let captchaText = "abc";
await client.decode({captcha: captcha_file, extra: {type: 0}}, (captcha) => {
if (captcha) {
console.log('Captcha ' + captcha['captcha'] + ' solved: ' + captcha['text']);
console.log("5");
captchaText = captcha['text'];
console.log("6");
};
});
console.log("7");
await page.$eval('#captchaField', el => el.value = captchaText);
console.log("8");
await page.click('#submitbtn');
console.log("9");
}
我正在像这样执行这个函数
(async () => {
// some puppeteer code
await solve_captcha(page, client, 'captcha > div', 'captcha');
// some puppeteer code
})();
这是我得到的输出
0
1
2
{"name":"captcha.jpg","type":"jpg","path":"./","fullPath":"./\\captcha.jpg"}
3
4captcha.jpg
7
(node:15000) UnhandledPromiseRejectionWarning: Error: Evaluation failed: ReferenceError: captchaText is not defined
(node:15000) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:15000) [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.
690.3853
Captcha 221820197 solved: XZ4xS9Lsw
5
6
我很困惑为什么在 4
之后它会跳过 client.decode
block ?另外,即使我已经明确定义了它,为什么我还是会收到 captchaText is not Defined
?以及如何正确执行?
最佳答案
async
和 await
关键字是管理 promise 的工具。
client.decode
接受回调(您没有对其调用 then()
),因此它可能不会返回 promise 。
因此 await
关键字不起作用。
您可以围绕回调函数创建一个 promise 。
进一步阅读:
关于javascript - 为什么javascript方法最后执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66060826/