2

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

MartenCatcher
  • 2,713
  • 8
  • 26
  • 39
DM Malkawi
  • 37
  • 2
  • 8
  • 3
    I know this doesn't help you at all, but honestly .. if you find a way to automate passing a captcha in a UI test, that captcha is - by definition - worthless. – Simon Jan 27 '20 at 07:09
  • 2captcha isn't bot automation, it's real people solving the puzzle for you – DM Malkawi Jan 27 '20 at 13:38
  • it doesn't matter if a human found the answer... if they detect you as a bot, the captcha won't solve in that browser session. Some will just allow you to endlessly solve puzzles, but you never pass. – pcalkins Jan 27 '20 at 22:54
  • Ive seen people do it though.. and on this site but the problem is i still cant get it to work and im really desperate – DM Malkawi Jan 28 '20 at 02:11

2 Answers2

3

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();
            }
        }
Ahmet Aziz Beşli
  • 1,280
  • 3
  • 19
  • 38
0

I think you should wait for response from API server. Something like this:

    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

    }
Bonum Ursus
  • 46
  • 1
  • 3
  • My code waits for the response by using the service.solveCaptcha(), and it always generates a responseToken, therefore it solves it.. but I need selenium to solve the puzzle through the responseToken generated by 2captcha – DM Malkawi Jan 27 '20 at 13:37
  • I also use 2captcha's API https://github.com/2captcha/2captcha-api-examples/tree/master/ReCaptcha%20v2%20API%20Examples/Java%20Example – DM Malkawi Jan 27 '20 at 13:39
  • any ideas? :( :( – DM Malkawi Jan 27 '20 at 19:31