How to read the text from image (captcha) by using Selenium WebDriver with Java How to read the text from image (captcha) by using Selenium WebDriver with Java selenium selenium

How to read the text from image (captcha) by using Selenium WebDriver with Java


Just to elaborate the previous answers, CAPTCHA as an acronym for "Completely Automated Public Turing test to tell Computers and Humans Apart".So, if "machine" can solve it, it's not really do it's job.

In order to solve it, there is something you can do - to use API of external services such as http://www.deathbycaptcha.com.You implementing their API, passing them the CAPTCHA and get in return the text. The average solving time i have observed is around 10-15 seconds.

Example for implementation (taken from here)

import com.DeathByCaptcha.AccessDeniedException;import com.DeathByCaptcha.Captcha;import com.DeathByCaptcha.Client;import com.DeathByCaptcha.SocketClient;import com.DeathByCaptcha.HttpClient;/* Put your DeathByCaptcha account username and password here.   Use HttpClient for HTTP API. */Client client = (Client)new SocketClient(username, password);try {    double balance = client.getBalance();    /* Put your CAPTCHA file name, or file object, or arbitrary input stream,       or an array of bytes, and optional solving timeout (in seconds) here: */    Captcha captcha = client.decode(captchaFileName, timeout);    if (null != captcha) {        /* The CAPTCHA was solved; captcha.id property holds its numeric ID,           and captcha.text holds its text. */        System.out.println("CAPTCHA " + captcha.id + " solved: " + captcha.text);        if (/* check if the CAPTCHA was incorrectly solved */) {            client.report(captcha);        }    }} catch (AccessDeniedException e) {    /* Access to DBC API denied, check your credentials and/or balance */}


Two problems.

  1. You have the wrong xpath so you getting a NoSuchElement exception.

  2. Even you had the right xpath, you would not be able to extract the text, as that would defeat the point if CAPTCHA


The whole purpose of CAPTCHA is to prevent automation from the UI! You may wanna use internal APIs for verifying the action.