Last active
October 28, 2021 04:16
-
-
Save Spring3/42629b7ab7ab2f557d89 to your computer and use it in GitHub Desktop.
Solve google recaptcha using 2captcha API and Selenium WebDriver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If I am not mistaken, it has to be imported. You can find out more about the package and class from the docs
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