2

I'm using puppeteer and https://www.deathbycaptcha.com/ to automatically solve a captcha.
Here is my function to solve the captcha.

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");
}

I'm executing this function like this

(async () => {
// some puppeteer code
await solve_captcha(page, client, 'captcha > div', 'captcha');
// some puppeteer code
})();

Here is the output I'm getting

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

I'm confused why after 4 it skips the client.decode block? Also why am I getting captchaText is not defined even when I've clearly defined it? And how to execute it correctly?

atin
  • 985
  • 3
  • 11
  • 28
  • `await` only does something useful when you await a promise. awaiting some asynchronous function that does not return a promise and uses a callback does NOTHING useful at all. You can't just throw a bunch of `await` statements in your code and expect them to do something useful. You HAVE to use `await` with promises if you want them to do anything useful. I have written this comment at lot lately. Somewhere a lot of people are getting the wrong idea about how to use `await`. – jfriend00 Feb 05 '21 at 09:39

1 Answers1

4

The async and await keywords are tools to manage promises.

client.decode takes a callback (you aren't calling then() on it) so it, presumably, doesn't return a promise.

The await keyword thus has no effect.

You can create a promise around your callback function.

Further reading:

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Thanks! But why am I getting the `captchaText is not defined` even when I've clearly defined it? – atin Feb 05 '21 at 09:56
  • `$eval` evaluates the function in the browser context not in Node. https://stackoverflow.com/questions/46088351/how-can-i-pass-variable-into-an-evaluate-function – Quentin Feb 05 '21 at 10:01