Skip to content

Instantly share code, notes, and snippets.

@Spring3
Last active October 28, 2021 04:16
Show Gist options
  • Save Spring3/42629b7ab7ab2f557d89 to your computer and use it in GitHub Desktop.
Save Spring3/42629b7ab7ab2f557d89 to your computer and use it in GitHub Desktop.
Solve google recaptcha using 2captcha API and Selenium WebDriver
//client = HttpClient
//captchaUserId = user id from http://2captcha.com
private String solveCaptcha(WebDriver driver) throws Exception{
WebElement captchaChallenge = driver.findElement(By.id("recaptcha_challenge_image"));
if (captchaChallenge != null){
String imageURL = captchaChallenge.getAttribute("src");
InputStream in = new BufferedInputStream(new URL(imageURL).openStream());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (int i; (i = in.read()) != -1;){
baos.write(i);
}
in.close();
baos.flush();
String base64Image = Base64.encodeBase64String(baos.toByteArray());
baos.close();
client.AddPostParam("body", base64Image);
client.AddPostParam("method", "base64");
client.AddPostParam("key", captchaUserId);
client.POST("http://2captcha.com/in.php");
if (client.responseContains("OK")){
System.out.println("Captcha image sent");
}
else{
System.err.println(client.getRawBody());
return null;
}
String key = client.getRawBody().substring(client.getRawBody().indexOf("|") + 1, client.getRawBody().length());
System.out.println("Captcha waiting key :" + key);
do{
client.Navigate(String.format("http://2captcha.com/res.php?key=%s&action=get&id=%s", captchaUserId, key));
Thread.sleep(1000);
} while(client.responseContains("NOT_READY"));
}
String captchaAnswer = null;
if (client.responseContains("OK")){
captchaAnswer = client.getRawBody().substring(client.getRawBody().indexOf("|") + 1, client.getRawBody().length());
System.out.println("Captcha solved: " + captchaAnswer);
}else{
System.err.println("Captcha was not solved");
System.err.println(client.getRawBody());
}
return captchaAnswer;
}
@prasu2307
Copy link

Can you please help me to resolve below errors:

  1. Error at line "InputStream in = new BufferedInputStream(new URL(imageURL).openStream());" ---> BufferedInputStream cannot be resolved to a type
  2. What is client variable? What is meant by //client = HttpClient?

@dmd2222
Copy link

dmd2222 commented Sep 7, 2019

I also dont get this with the client... I have only tried it 10 minutes... Best regards

@Spring3
Copy link
Author

Spring3 commented Sep 7, 2019

Can you please help me to resolve below errors:

  1. Error at line "InputStream in = new BufferedInputStream(new URL(imageURL).openStream());" ---> BufferedInputStream cannot be resolved to a type

If I am not mistaken, it has to be imported. You can find out more about the package and class from the docs

  1. What is client variable? What is meant by //client = HttpClient?

A class, responsible for handling the http requests. In my example, that was a custom implementation that I could not share. You can pretty much get the idea of what each function was meant to do by the function's name.

P.S. I switched to Node.js / React stack more than 3 years ago and haven't written a single line of code in java since then, so this example might be outdated or needs to be updated. I also do not follow the updates from java, so mentioned classes can potentially work differently / have different api / be removed from the core by this time.

Thanks for the interest in the gist, but I am not sure I will be able to help you out if you encounter any problems with this example of implementation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment