Java Selenium + 2Captcha +提交表单 [英] Java Selenium + 2Captcha + Submit Form

查看:430
本文介绍了Java Selenium + 2Captcha +提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试使某些过程自动化检出图像.

Hello i am trying to automate some process 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中,但是它没有提交表单.

I have got site_key and api_key , now i am sending api_key + site_key and it is returning me response_token, i have added returned response token into g-recaptcha-response but it is not submitting form.

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

what i want is that : i can solve captcha and submit form .

这是我当前的Java代码:

Here is my current java code :

 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. .请查看屏幕截图

it is clicking on button but , it shows Please select all matching images. . please check out screenshot

推荐答案

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

All you need to do is submit it like this:

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

也不要忘记检查此ID:验证码形式",它可以不同

also don't forget to check this ID : "captcha-form", it can be different

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

To reach to element "recaptcha-verify-button":

从API获得响应后;

After you got the response from the API;

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

WebElement frameElement = driver.findElement(frame);

driver.switchTo.frame(frameElement);

然后您可以执行脚本.最后,对于您的脚本,如果验证码表单是按钮

then you can execute your script. Finally, for your script if your captcha form is a button

无法调用Submit();

cannot call submit();

可以调用click();

can call click();

最终答案:

也请检查以下内容:js.executeScript("widgetVerified('TOKEN');");

要找到名为widgetVerified()的功能,请在控制台中运行此代码.

To find the function called widgetVerified() please run this code in the console.

___grecaptcha_cfg.clients[0]

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

this will return a json, inside of that json try to find the callback function in @Awais case it was wigdetVerified(e)

警告:请勿使用任何adblocker

Warn : Don't use any adblocker

这篇关于Java Selenium + 2Captcha +提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆