【发布时间】:2021-05-09 15:37:00
【问题描述】:
我正在使用 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 块?另外,为什么即使我已经明确定义了captchaText is not defined?以及如何正确执行?
【问题讨论】:
-
await仅在您等待承诺时才做一些有用的事情。等待一些不返回承诺并使用回调的异步函数根本没有用处。您不能只在代码中抛出一堆await语句并期望它们做一些有用的事情。如果您希望他们做任何有用的事情,您必须使用带有承诺的await。我最近在很多地方写了这个评论。很多人对如何使用await有错误的想法。
标签: javascript node.js asynchronous async-await puppeteer