How to Break WordPress Captcha using Selenium | Prove Your Humanity

Getting on the bandwagon to help you understand How to Break WordPress Captcha using Selenium. Sit tight and keep following these easy to do steps.

You might have heard that it is impossible to break CAPTCHA using automation, that the reason CAPTCHA was created was to make sure that robots don’t take over or to make it difficult for an automation script to understand it. That CAPTCHA cannot be solved or broken by an automation script. But that’s not entirely true. While there are some hard to beat CAPTCHAs out there, there also exist some pretty simple ones that can be broken quite easily. For instance, the one that Jetpack plugin offers on WordPress often presents.

If you have been working with WordPress for a while, you already know that frequently logging in and out of WordPress using an Automation tool might require you to handle a Captcha that looks something like this:

How to Break WordPress Captcha using Selenium prove your humanity captcha by jetpack

Isn’t that one of the easiest calculations to be made? But that’s only when it is supposed to be done manually.

If only there was a way to dynamically capture that data, I don’t know, turn it into Integers, and come up with their result in the box. Hmmm….Doesn’t that already sound doable?

Also, I have run various experiments to find out that generally, the operator that WordPress uses is ‘+’ i.e. addition and that the numbers that are used are generally single digits. Not that, if it doesn’t, it should stop you or anything. It shouldn’t stop you from breaking it any further, right? Whatever the algorithm they have used, you can use the same ones to break it down? Can’t you?

debug meme

Keeping all of these trivial things in mind, let’s try to break the WordPress Captcha using Selenium.

How to Break WordPress Captcha using Selenium

So you have a code in place for entering login credentials. It was working fine hitherto.

Maybe it looked like this:

//Punching in Credentials
         driver.findElement(By.xpath("//[@id=\"user_login\"]")).sendKeys(Username);         driver.findElement(By.xpath("//[@id=\"user_pass\"]")).sendKeys(Password);

//Clicking on Log In button
driver.findElement(By.xpath("//*[@id=\"wp-submit\"]")).click();

where Username and Password are nothing but String with your username and password.

Now as you were trying to run your code multiple times, suddenly popped up a “Prove your humanity” CAPTCHA. Such audacity!

Now what would you do?

Come up with an if statement block to deal with the CAPTCHA case, right? Easy, huh! But what would go into this “if block”?

Now if you inspect the source code behind that special pop-up you would see that it is a separate div style right after the login form.

An XPath that looks something like:

//*[@id="loginform"]/div

would be able to identify it.

Let’s see it in action:

Cool right?

Time to write its code now:

Steps to Break WordPress Captcha

Let’s start with leveraging the above find. We can use “Find Elements” for a if element available logic. So here goes our first step:

Step 1: Type the following code right after you have logged in to your wp-admin page.

List<WebElement> captchaFormList = driver.findElements(By.xpath("//*[@id=\"loginform\"]/div"));

The above code will help you with the CAPTCHA’s visibility, to successfully handle it in an if condition. So even if the code doesn’t find the CAPTCHA, it would still move on.

Step 2: Follow it by the remaining things:

if(captchaFormList.size()!=0) {

//Copying the CAPTCHA question

String question = driver.findElement(By.xpath("//*[@id=\"loginform\"]/div/span")).getText();

}

The if condition is simply to handle the situation when CAPTCHA shows. In the above code we have also tried to grab the question in CAPTCHA so that we can further break it down.

For example, the question grabbed in the above example would be:

0      +      4      = 

It should be well noticed that there are a lot of blank spaces between these figures. Firstly, we will try to remove these blank spaces in order to sunder out our numbers.

Splitting values with Space

Step 3: We can make use of split in order to do that. Simply type the following to get rid of the spaces:

String []sNew = question.split("\\s+");

By using the above array, we can put each of the digits in a new index. The regex of \\s+ removes all the spaces present inside the String. You can check out the chapter on Regex to get a better understanding of how regular expressions work.

Step 4: Guessed right, a for loop is coming to see those indices:

for(int i=0; i < sNew.length; i++) {
       System.out.println(sNew[i]);
}

So the code we have so far is:

List<WebElement> captchaFormList = driver.findElements(By.xpath("//*[@id=\"loginform\"]/div")); 

if(captchaFormList.size()!=0) {

//Copying the CAPTCHA question

String question = driver.findElement(By.xpath("//*[@id=\"loginform\"]/div/span")).getText();

String []sNew = question.split("\\s+");

 for(int i=0; i < sNew.length; i++) {

        System.out.println(sNew[i]);
     
    }

}

Now, what do we want? We want to be able to decipher the number and ignore the operators from our WordPress question.

Step 5: To do that we shall make use of the following if condition:

if(sNew[i].matches("[0-9]")) {
}

The above line means, if the individual indices hold a numeric value it would enter the if block. The rest i.e. ‘+’ and ‘=’ shall be ignored.

The Addition of Numbers Logic

Step 6: Inside the above block, we will put the logic of adding both the numbers that will be grabbed. This should do it:

result += Integer.valueOf(sNew[i]);

where we will initialize the result as int.

int result = 0;

Step 5: Now outside the block we can print the result using:

System.out.println("Final answer is " + result);

Use SendKeys to Enter Result in the Box

Now we have to use that same “result” variable in the small box provided for answer.

Let’s find out the XPath of the box first. Okay, this shall do:

//*[@id=\"jetpack_protect_answer\"]

Step 6: We can use this variable result in the box as:

driver.findElement(By.xpath("//*[@id=\"jetpack_protect_answer\"]")).sendKeys(String.valueOf(result));

where we need to once again convert the result integer into String.

So our final code will look something like this:

int result = 0;

List<WebElement> captchaFormList = driver.findElements(By.xpath("//*[@id=\"loginform\"]/div")); 

if(captchaFormList.size()!=0) {

//Copying the CAPTCHA question

String question = driver.findElement(By.xpath("//*[@id=\"loginform\"]/div/span")).getText();

String []sNew = question.split("\\s+");

 for(int i=0; i < sNew.length; i++) {

        System.out.println(sNew[i]);

             if(sNew[i].matches("[0-9]")) {
                   result += Integer.valueOf(sNew[i]);
           }
    }
System.out.println("Final answer is " + result);

}

We are done! That’s the chunk of code that can now break the addition logic of a simple WordPress Captcha. Go ahead and try it!

Now the question lies what if the operator is something else? What if it is subtraction, division or multiplication.

What would you do then?

Well, you can always create separate methods for each one of them and handle them. I will leave you to figure it out for yourself. That’s the homework homie!

Did you like this How to Break WordPress Captcha using Selenium tutorial? Tell us in the comments section.

Scottshak

Poet. Author. Blogger. Screenwriter. Director. Editor. Software Engineer. Author of "Songs of a Ruin" and proud owner of four websites and two production houses. Also, one of the geekiest Test Automation Engineers based in Ahmedabad.

You may also like...

Leave a Reply