Java Selenium + 2Captcha + 提交表单

Java Selenium + 2Captcha + Submit Form

您好,我正在尝试使某些流程自动化 here . i am using 2captch to solve captcha , please check out image。

我有 site_key 和 api_key ,现在我发送 api_key + site_key 它正在返回我 response_token,我已经添加返回响应令牌进入 g-recaptcha-response 但它没有提交表单。

我想要的是:我可以解决验证码并提交表单。

这是我当前的 java 代码:

 System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
    ChromeDriver driver;
    driver = new ChromeDriver();
    driver.manage().deleteAllCookies();
    driver.manage().window().maximize();
    driver.get("https://id.sonyentertainmentnetwork.com/signin/?client_id=fe1fdbfa-f1a1-47ac-b793-e648fba25e86&redirect_uri=https://secure.eu.playstation.com/psnauth/PSNOAUTHResponse/pdc/&service_entity=urn:service-entity:psn&response_type=code&scope=psn:s2s&ui=pr&service_logo=ps&request_locale=en_GB&error=login_required&error_code=4165&error_description=User+is+not+authenticated&no_captcha=false#/signin?entry=%2Fsignin");
    Thread.sleep(5000);

    driver.findElement(By.xpath("//input[@title='Sign-In ID (Email Address)']")).sendKeys("email");
    Thread.sleep(2000);
    driver.findElement(By.xpath("//input[@title='Password']")).sendKeys("password");
    Thread.sleep(2000);
    driver.findElement(By.xpath("//button[@class='primary-button row-button text-button touch-feedback']")).click();
    Thread.sleep(3000);
    By captcha = By.xpath("//iframe[@title='recaptcha challenge']");
    String src = driver.findElement(captcha).getAttribute("src");
    String key = getKey(src);
    System.out.println(key);

    String apiKey = "API_KEY";
    String googleKey = key;
    String pageUrl = "https://id.sonyentertainmentnetwork.com/signin/?client_id=fe1fdbfa-f1a1-47ac-b793-e648fba25e86&redirect_uri=https://secure.eu.playstation.com/psnauth/PSNOAUTHResponse/pdc/&service_entity=urn:service-entity:psn&response_type=code&scope=psn:s2s&ui=pr&service_logo=ps&request_locale=en_GB&error=login_required&error_code=4165&error_description=User+is+not+authenticated&no_captcha=false#/signin?entry=%2Fsignin";
    String proxyIp = "183.38.231.131";
    String proxyPort = "8888";
    String proxyUser = "username";
    String proxyPw = "password";


    TwoCaptchaService service = new TwoCaptchaService(apiKey, googleKey, pageUrl, proxyIp, proxyPort, proxyUser, proxyPw, ProxyType.HTTP);

    try {
        String responseToken = service.solveCaptcha();
        System.out.println("The response token is: " + responseToken);
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("document.getElementById(\"g-recaptcha-response\").innerHTML = \'"+responseToken+"\';");
    } catch (InterruptedException e) {
        System.out.println("ERROR case 1");
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println("ERROR case 2");
        e.printStackTrace();
    }

更新代码:

          js.executeScript("document.getElementById(\"g-recaptcha-response\").innerHTML = \'" + responseToken + "\';");
        Thread.sleep(500);
        WebElement frameElement = driver.findElement(captcha);
        driver.switchTo().frame(frameElement);         
        js.executeScript("document.getElementById('recaptcha-verify-button').click();");

它正在点击按钮,但是,它显示 Please select all matching images. 。请查看 screenshot

您需要做的就是像这样提交:

js.executeScript("document.getElementById('g-recaptcha-response').innerHTML='" + responseToken + "';");
Thread.sleep(500);
js.executeScript("document.getElementById('captcha-form').submit();");

也不要忘记检查此 ID:"captcha-form",它可以不同

到达元素 "recaptcha-verify-button":

收到 API 的回复后;

By frame = By.xpath("//iframe[@title='recaptcha challenge']");

WebElement frameElement = driver.findElement(frame);

driver.switchTo.frame(frameElement);

然后你就可以执行你的脚本了。最后,对于您的脚本,如果您的验证码表单是一个按钮

cannot call submit();

can call click();

最终答案:

同时检查一下:js.executeScript("widgetVerified('TOKEN');");

要找到名为 widgetVerified() 的函数,请在控制台中 运行 此代码。

___grecaptcha_cfg.clients[0]

这将 return 一个 json,在里面 json 尝试在 @Awais 的情况下找到回调函数 wigdetVerified(e)

Warn : Don't use any adblocker