Solve reCAPTCHA 2 in Selenium

How to solve any captcha using Selenium and AntiCaptcha plugin solver. Selenium installation, setup and examples of the API requests to the plugin. Code snippets are written in Python.

On the web page presented information about Selenium and AntiCaptcha plugin captcha solving. Any captcha including reCAPTCHA v2 can be solved this way.
We'll pass through the installation process step by step and we'll learn how to use the Selenium with our extension. Also we are going to solve a Recaptcha 2 in demo mode.
For whose, who doesn't know what Selenium is — this is a tool which can automatic run and surf a browser.

If you already are familiar with Selenium we recommend to jump into the captcha solving example code.

Attention! Anti-Captcha.com account key is required for its work. I.e. this captcha solving method isn't free, but it's not expensive and it's really conconvenient!

More information about our extension with examples you can find on the AntiCaptcha plugin main page.

The demo code is presented in the Python language, since it's most popular by using with Selenium.
Average computed literacy required.
You should understand a programming language and have minimal CLI (also known as "command line" or "terminal") experience.

The Python installation

We'll use a Python 3.
There is a lot of text and video guides about Python installation. We won't repeat it here, I'll just pass a few links where you can find all the information:
Video instructions of how to install Python
Python installation text instructions

In general you need to set the Python interpreter up and register it in PATH, so you could run it in Terminal with python3 or python.exe command.
For Windows it's recommended to check "Add python.exe to Path" box while installing. So you won't have to type the whole path to the python.exe and pip3.exe when you want to run those two. PIP is the Python Package Installer, i.e. package manager for Python libraries, we will need this to install Selenium later.
In Linux after installing you'll have the python3 command, for Windows it would be python.exe (without "3" in the end).

Selenium and WebDriver installation

Things get a bit harder here. Beside the Selenium itself we also need to set up the WebDriver for browser.
It's an intermediate layer between Selenium and the browser. For a particular browser you need a corresponding WebDriver, we'll use a driver for Chrome in our example.
There is many articles of how do it on the Internet. They all kind of similar but in most cases redundant and confusing.
Therefore we brign you here a simplified version in hope, that it would be useful for beginners.
For Linux users: sometimes Python does not install the PIP package manager automatically, if so - you simply install it from the package manager of your Linux dist. Example for Ubuntu: sudo apt install python3-pip.

Selenium installing for Python

After you got Python you'll also getting the special package manager (the Python classes collection online installer) called pip3.
It allows you to quickly install the latest Selenium version by calling a simple console (Terminal or Cmd.exe) command.

pip3 install selenium
For Windows it would be:
pip3.exe install selenium

That's it.
You can test if it's installed by calling python3 or python.exe and entering there:
from selenium import webdriver
If there is not error appeared after this code it means that Selenium got installed successfully. You can exit the Python interpreter now by pressing CTRL+D in Linux and CTRL+Z —> Enter for Windows.

In Windows there might appear a "pip3.exe not found" type of error. Which means that the python wasn't registered in a global PATH variable.
You need to return the Python installing step and check a "Add python.exe to Path" checkbox. Google it in case of difficulties.

The WebDriver installing

One more time, WebDriver is an additional layer that allows us to interact with the browser.
Like this:
Python uses Selenium as a library and calls its methods and objects.
Selenium connects to the WebDriver of a particular browser (Chrome, Firefox, etc)
The WebDriver runs a regular Chrome or Firefox browser, which we use in everyday life, and translates the commands from Selenium into it. Accordingly, the browser itself must be installed in advance (most likely this has already been done, since you are reading this page through the browser).

The WebDriver itself is just a file, for example chromedriver for Chrome or geckodriver for Firefox. Which will be passed to the Selenium library on initialisation.
So in fact it's installing process boils down to saving this file in a particular place.
Do not forget to choose the version of WebDriver the same as the version of the browser you have installed: just open your Chrome or Firefox browser (depending on which browser you are going to use with Selenium) and check the version.
For example for the Chrome 68 you need to get the WebDriver of version 68.

Here are the links with more detailed information:
Selenium webdriver · PyPI
Install Web Drivers

The fact that installation was successful or not can be checked in the Python interepter. Run python3 or python.exe in console and type there:
For Windows: from selenium import webdriver browser = webdriver.Chrome("C:\\place\\where\\is\\chromedriver.exe"
For Linux from selenium import webdriver browser = webdriver.Chrome('/home/place/where/is/chromedriver')
If there is no error appears and a browser window is opened — this means the installation process was successful.

In many tutorials is recommended to register a downloaded WebDriver in the PATH system variable. Or just to put the driver in one of the PATH directories.
We also recommend to do it that way. It will allow you to not to provide the full path to the WedDriver file in commands above while initialization.
Google how to do it in different operating systems.

Solving our first captcha in Selenium

AntiCaptha plugin download

First of all we need to go to a plugin downloading web page, choose your browser where we'll solve captcha (Chrome, Firefox) and download a corresponding file with the plugin code.
For Chrome it would be CRX or ZIP. For Firefox — XPI format, the ZIP can also fit, but XPI is much better.
A downloaded file should be placed in any project directory, we'll provide a full path to it in our python code.

Python captcha solving example

You can read info about the Selenium browser automation first by the following link.
Python Selenium Getting Started
We will focus on a our specific example.

We will put an emphasis on one point. For the plugin normal work the Anti-Captcha.com API account key is required.
Usually it can be set manually in the plugin Options. But it's not possible with Selenium.
For setting up the Anti-Captcha key we use the plugin API messages sending and setOptions method.
Method acp_api_send_request in the snippet below does just that in the line 43.

We also notice that beside the method mentioned above, there is also an explicit way to set the Anti-Captcha service API key up. Which one is better is your choice.

The code belows is provided with comments in English.

# Import libraries and objects from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait import time import json # The API messages sending directly to the plugin # For example for the anti-captcha.com API key init which is required for the plugin work # Works only on the normal HTML web page # https://antcpt.com/blank.html in our case # Won't work on pages like about:blank etc def acp_api_send_request(driver, message_type, data={}): message = { # this receiver has to be always set as antiCaptchaPlugin 'receiver': 'antiCaptchaPlugin', # request type, for example setOptions 'type': message_type, # merge with additional data **data } # run JS code in the web page context # preceicely we send a standard window.postMessage method return driver.execute_script(""" return window.postMessage({}); """.format(json.dumps(message))) # Init the chrome options object for connection the extension options = webdriver.ChromeOptions() # A full path to CRX or ZIP or XPI file which was downloaded earlier options.add_extension('/full/path/to/plugin/file/anticaptcha.crx') # Run the browser (Chrome WebDriver) with passing the full path to the downloaded WebDriver file browser = webdriver.Chrome('/home/full/path/to/chromedriver', options=options) # Go to the empty page for setting the API key through the plugin API request browser.get('https://antcpt.com/blank.html') # Setting up the anti-captcha.com API key # replace YOUR-ANTI-CAPTCHA-API-KEY to your actual API key, which you can get from here: # https://anti-captcha.com/clients/settings/apisetup acp_api_send_request( browser, 'setOptions', {'options': {'antiCaptchaApiKey': 'YOUR-ANTI-CAPTCHA-API-KEY'}} ) # 3 seconds pause time.sleep(3) # Go to the test form with reCAPTCHA 2 browser.get('https://antcpt.com/rus/information/demo-form/recaptcha-2.html') # Test input browser.find_element_by_name('demo_text').send_keys('Test input') # Most important part: we wait upto 120 seconds until the AntiCaptcha plugin indicator with antigate_solver class # gets the solved class, which means that the captcha was successfully solved WebDriverWait(browser, 120).until(lambda x: x.find_element_by_css_selector('.antigate_solver.solved')) # Sending form browser.find_element_by_css_selector('input[type=submit]').click()

Save this code in a captcha_solve.py file and execute the next console command:

python3 captcha_solve.py

If everything goes well then the browser window will be opened and the magic captcha solving begins =)

The way to explicitly register the Anti-Captcha service key

At the request of our users, we have implemented an alternative way to specify the API key — by writing it in the configuration file.
To do this, you need to download the plugin code in ZIP format and unzip this archive. Inside the js directory you can find a config_ac_api_key.js file.
Open it in any text editor and put your anti-captcha.com API key as a value for the antiCapthaPredefinedApiKey variable.
For example:
var antiCapthaPredefinedApiKey = '12345678901234567890123456789012';

Pay your attention, that besides the variable key you can also find a defaultConfig variable which contains all the plugin options defaults. Be free to edit them as you like. A description for any option can be seen in the plugin API article, in the setOptions method.
With one difference that in the article all the options are presented in a camelCase but in the config file we use underline_case (useRecaptchaPrecaching ~ use_recaptcha_precaching).

Last thing we need to do — pack the whole code back to the ZIP archive and specify a path to this new file in the add_extension method (see our file captcha_solve.py above).
And of course lines starting from 37 to 50 in our example are not needed anymore.

We hope this tutorial was useful for you. If there any questions, please don't hesitate to use our Feedback form, we will respond you!