使用 Selenium 和 2Captcha 解决图片验证码 (JAVA)
Solving Picture Captcha With Selenium and 2Captcha (JAVA)
我一直在努力使用 selenium 解决验证码,java,2captcha 的 api。
点击验证按钮没有解出图片,没有报错..
这是我的代码:
private void solveCaptcha(String apiKey) {
String googleKey = "6Lcsv3oUAAAAAGFhlKrkRb029OHio098bbeyi_Hv";
String pageUrl = "https://secure.runescape.com/m=weblogin/loginform?theme=oldschool&mod=www";
TwoCaptchaService service = new TwoCaptchaService(apiKey, googleKey, pageUrl);
try {
String responseToken = service.solveCaptcha();
By frame = By.xpath("//iframe[@title='recaptcha challenge']");
WebElement frameElement = driver.findElement(frame);
driver.switchTo().frame(frameElement);
System.out.println("Solved and Generated Response Token: " + responseToken);
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('recaptcha-token').innerHTML = '" + responseToken + "';");
Thread.sleep(500);
js.executeScript("document.getElementById('recaptcha-verify-button').click();");
} catch (InterruptedException e) {
System.out.println("ERROR case 1");
e.printStackTrace();
} catch (IOException e) {
System.out.println("ERROR case 2");
e.printStackTrace();
}
}
非常感谢您的帮助
我认为您应该等待 API 服务器的响应。像这样:
File imgFile = new File("path to img");
RuCaptcha.API_KEY = "fdg998fsffdbg9b0bsd0sdf";
String CAPCHA_ID;
String decryption;
String response = RuCaptcha.postCaptcha(imgFile);
if (response.startsWith("OK")) {
CAPCHA_ID = response.substring(3);
while (true){
response = RuCaptcha.getDecryption(CAPCHA_ID);
if(response.equals(RuCaptcha.Responses.CAPCHA_NOT_READY.toString())){
Thread.sleep(5000);
continue;
}else if(response.startsWith("OK")){
decryption = response.substring(3);
break;
}else {
//error code
}
}
//your code
}
试试这个。
private void solveCaptcha(String apiKey) {
String googleKey = "6Lcsv3oUAAAAAGFhlKrkRb029OHio098bbeyi_Hv";
String pageUrl = "https://secure.runescape.com/m=weblogin/loginform?theme=oldschool&mod=www";
TwoCaptchaService service = new TwoCaptchaService(apiKey, googleKey, pageUrl);
try {
String responseToken = service.solveCaptcha();
System.out.println("Solved and Generated Response Token: " + responseToken);
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('g-recaptcha-response').innerHTML = '" + responseToken + "';");
Thread.sleep(500);
js.executeScript("onSubmit()");
} catch (InterruptedException e) {
System.out.println("ERROR case 1");
e.printStackTrace();
} catch (IOException e) {
System.out.println("ERROR case 2");
e.printStackTrace();
}
}
我一直在努力使用 selenium 解决验证码,java,2captcha 的 api。
点击验证按钮没有解出图片,没有报错..
这是我的代码:
private void solveCaptcha(String apiKey) {
String googleKey = "6Lcsv3oUAAAAAGFhlKrkRb029OHio098bbeyi_Hv";
String pageUrl = "https://secure.runescape.com/m=weblogin/loginform?theme=oldschool&mod=www";
TwoCaptchaService service = new TwoCaptchaService(apiKey, googleKey, pageUrl);
try {
String responseToken = service.solveCaptcha();
By frame = By.xpath("//iframe[@title='recaptcha challenge']");
WebElement frameElement = driver.findElement(frame);
driver.switchTo().frame(frameElement);
System.out.println("Solved and Generated Response Token: " + responseToken);
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('recaptcha-token').innerHTML = '" + responseToken + "';");
Thread.sleep(500);
js.executeScript("document.getElementById('recaptcha-verify-button').click();");
} catch (InterruptedException e) {
System.out.println("ERROR case 1");
e.printStackTrace();
} catch (IOException e) {
System.out.println("ERROR case 2");
e.printStackTrace();
}
}
非常感谢您的帮助
我认为您应该等待 API 服务器的响应。像这样:
File imgFile = new File("path to img");
RuCaptcha.API_KEY = "fdg998fsffdbg9b0bsd0sdf";
String CAPCHA_ID;
String decryption;
String response = RuCaptcha.postCaptcha(imgFile);
if (response.startsWith("OK")) {
CAPCHA_ID = response.substring(3);
while (true){
response = RuCaptcha.getDecryption(CAPCHA_ID);
if(response.equals(RuCaptcha.Responses.CAPCHA_NOT_READY.toString())){
Thread.sleep(5000);
continue;
}else if(response.startsWith("OK")){
decryption = response.substring(3);
break;
}else {
//error code
}
}
//your code
}
试试这个。
private void solveCaptcha(String apiKey) {
String googleKey = "6Lcsv3oUAAAAAGFhlKrkRb029OHio098bbeyi_Hv";
String pageUrl = "https://secure.runescape.com/m=weblogin/loginform?theme=oldschool&mod=www";
TwoCaptchaService service = new TwoCaptchaService(apiKey, googleKey, pageUrl);
try {
String responseToken = service.solveCaptcha();
System.out.println("Solved and Generated Response Token: " + responseToken);
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('g-recaptcha-response').innerHTML = '" + responseToken + "';");
Thread.sleep(500);
js.executeScript("onSubmit()");
} catch (InterruptedException e) {
System.out.println("ERROR case 1");
e.printStackTrace();
} catch (IOException e) {
System.out.println("ERROR case 2");
e.printStackTrace();
}
}