Search code examples
javaseleniumrecaptchacaptcha2captcha

Solving Picture Captcha With Selenium and 2Captcha (JAVA)


I have been struggling with solving captcha using selenium, java, 2captcha's api.

It clicks the verify button but doesn't solve the picture, no errors pop out..

Here's my 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();
        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();
    }
}

I'd really appreciate the help


Solution

  • Try this one.

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