【问题标题】:Solving Picture Captcha With Selenium and 2Captcha (JAVA)用 Selenium 和 2Captcha (JAVA) 解决图片验证码
【发布时间】:2020-05-12 12:14:37
【问题描述】:

我一直在努力解决使用 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();
    }
}

非常感谢您的帮助

【问题讨论】:

  • 我知道这对你一点帮助都没有,但老实说......如果你找到一种方法在 UI 测试中自动传递验证码,那么验证码 - 根据定义 - 毫无价值。
  • 2captcha 不是机器人自动化,而是真人为您解决难题
  • 人类是否找到答案并不重要......如果他们将您检测为机器人,验证码将无法在该浏览器会话中解决。有些只会让你无休止地解决难题,但你永远不会通过。
  • 我已经看到人们这样做了.. 在这个网站上,但问题是我仍然无法让它工作,我真的很绝望

标签: java selenium recaptcha captcha 2captcha


【解决方案1】:

试试这个。

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

【讨论】:

  • 谢谢。我回家后试试。非常感谢您的回复。
  • 谢谢你,上帝保佑你
  • 感谢代码真的很有帮助,你能告诉我在哪里可以找到 java 的 jar 依赖项吗?
【解决方案2】:

我认为您应该等待 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

    }

【讨论】:

  • 我的代码使用service.solveCaptcha()等待响应,它总是生成一个responseToken,因此它解决了它..但我需要selenium通过2captcha生成的responseToken来解决这个难题
  • 有什么想法吗? :( :(
猜你喜欢
  • 2022-12-15
  • 2021-02-04
  • 2021-10-22
  • 2020-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-26
  • 2020-01-18
相关资源
最近更新 更多