Java Selenium + 2Captcha + Submit Form

醉酒当歌 提交于 2019-12-10 10:26:13

问题


Hello i am trying to automate some process here . i am using 2captch to solve captcha , please check out image .

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 .

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

UPDATED CODE :

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

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


回答1:


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

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

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

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

you

cannot call submit();

you

can call click();

Final Answer:

Also check this: js.executeScript("widgetVerified('TOKEN');");

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

___grecaptcha_cfg.clients[0]

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

Warn : Don't use any adblocker



来源:https://stackoverflow.com/questions/57980022/java-selenium-2captcha-submit-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!