Search code examples
javaselenium2captcha

Java Selenium 2Captcha


i am trying to automate some process on ( here ) using Selenium with java . WHenever i press on signin button , it displays a captcha to select images like [this] .

i was reading about 2captcha to resolve this issue , they are asking for sitekey , can any one help me to get sitekey for this site`?

or is there any more better solution for this problem ?

Here is my java code that initiate recaptcha :

     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(2000);
     driver.findElement(By.xpath("//input[@title='Sign-In ID (Email Address)']")).sendKeys("[email protected]");
     Thread.sleep(2000);
     driver.findElement(By.xpath("//input[@title='Password']")).sendKeys("131313aa");
     Thread.sleep(2000);
     driver.findElement(By.xpath("//button[@class='primary-button row-button text-button touch-feedback']")).click();

Solution

  • For the captcha issue, you need to use any API to solve that.

    If already have one and looking for the site key of the captcha:

    By captcha = By.xpath("//iframe[@title='recaptcha challenge']");
    String src = driver.findElement(captcha).getAttribute("src");
    String key = getKey(src);
    
    public String getKey(String src){
        String x = src;
        String y = x.substring(x.indexOf("&k=")+3);
        String key = y.substring(0, StringUtils.ordinalIndexOf(y,"&",1));
        return key;
    }
    

    as you can see I am able to find it by the xpath selector, just go to developer console and check it. I don't know why it is not working for you.

    also try to get it with xpath: "//iframe"

    as