Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I display a google captcha in java?

Tags:

java

I just tried to create a program with HTMLUnit, which can log into a website (It can do more, of course, but that is the most important thing about it) with user entered data. Unfortunately, the website always redirects me to a website, where I have to enter a google captcha to continue. Here is my code:

public static void main (String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException, InterruptedException{
        final WebClient webClient = new WebClient(BrowserVersion.CHROME);
        final HtmlPage page1 = webClient.getPage("http://some.website/login");
        final HtmlForm form = page1.getFirstByXPath("//*[@id=\"section-main\"]/div/div[4]/div[2]/div[8]/table/tbody/tr/td/div/div/div/div/table/tbody/tr/td[2]/form");
        final HtmlSubmitInput button = form.getInputByValue("Login");
        final HtmlTextInput email = form.getInputByName("username");
        final HtmlPasswordInput password = (HtmlPasswordInput) form.getByXPath("//*[@id=\"section-main\"]/div/div[4]/div[2]/div[8]/table/tbody/tr/td/div/div/div/div/table/tbody/tr/td[2]/form/div[4]/input").get(0);
        email.setValueAttribute("[email protected]");
        password.setValueAttribute("aPassword");
        final HtmlPage page2 = button.click();
    }

It does not work, it returns a NullPointerException for the button, email and password, since it can't find the form. I know JDownloader has built something similar, but it is not available on the download page. So, my question is: How do I display a captcha to the user and let them enter the words?

like image 275
Mister X Avatar asked Dec 07 '25 21:12

Mister X


1 Answers

You need to parse page with captcha and get link to the captcha image. Later on you need to fetch the content of image data and create BufferedImage with ImageIO. Last step would be creating a window JFrame and show captcha for the user. Perfect solution would be to prompt for imput and post the answer. Yes it is possible, I have implemented exactly the same solution in the past.

like image 51
Antoniossss Avatar answered Dec 12 '25 07:12

Antoniossss