Skip to content

Anticaptcha Actions

Anticaptcha actions integrate Marvin with the Anti-Captcha service so it can solve different captcha types automatically.

To use these actions you must have an Anti-Captcha account and a valid API key (anticaptcha_key).

Important: ReCaptcha/hCaptcha solutions usually expire quickly (about 2 minutes). Solve the captcha as the last step before submitting the form or validating the page.

Return values (state variables) - Actions expose return values as state variables that can be read by the integrator. - To read a returned value, assign a local variable from the state variable using the $ prefix. The name must match exactly the key returned by the action.

Generic example:

# call the action
anticaptcha.recaptcha_v2(browser, "MY_KEY")

# read the returned token
g_recaptcha_response = $g_recaptcha_response

Actions

The actions below are available in Marvin:

anticaptcha.recaptcha_v2

Solves ReCaptcha v2 found on the current page.

Parameters: - browser — the browser/page object where the ReCaptcha is present. - anticaptcha_key — the Anti-Captcha account key (string). - callback_function (optional) — name of the JavaScript callback function used by the page to validate the ReCaptcha. If the page validates via POST, this parameter is not required.

Return (state variable): - g_recaptcha_response — the solution token (string).

Usage example:

# call action
anticaptcha.recaptcha_v2(browser, "MY_KEY")

# read returned token
g_recaptcha_response = $g_recaptcha_response

Exceptions: - CaptchaNotFound — raised when the ReCaptcha v2 cannot be located on the page, when more than one ReCaptcha is detected, or if there is an error executing the callback.

Notes: - The site key (sitekey) is extracted automatically from the ReCaptcha iframe when possible.

anticaptcha.hcaptcha

Solves hCaptcha found on the current page.

Parameters: - browser — the browser/page object where the hCaptcha is present. - anticaptcha_key — the Anti-Captcha account key (string). - callback_function (optional) — name of the JavaScript callback function used by the page. - hcaptcha_key (optional) — the hCaptcha sitekey; when provided it will be used instead of attempting automatic detection.

Return (state variable): - h_recaptcha_response — the solution token (string).

Usage example:

# call action
anticaptcha.hcaptcha(browser, "MY_KEY")

# read returned token
h_recaptcha_response = $h_recaptcha_response

# --- passing hcaptcha_key explicitly ---
anticaptcha.hcaptcha(browser, "MY_KEY", hcaptcha_key="SITEKEY_FROM_PAGE")

Exceptions: - CaptchaNotFound — raised when the hCaptcha cannot be found on the page or there is an error executing the callback.

anticaptcha.image_captcha

Solves an image captcha from a local file.

Parameters: - anticaptcha_key — the Anti-Captcha account key (string). - img_file — path to the image file containing the captcha.

Return (state variable): - captcha — the solved text (string).

Usage example:

# solve local image
anticaptcha.image_captcha("MY_KEY", "C:/temp/captcha.png")

# read result
captcha = $captcha

Exceptions: - CaptchaNotFound — raised when the solution could not be obtained from the service.

How to find the ReCaptcha callback function

You can find the ReCaptcha callback function in two common ways:

  • HTML element using g-recaptcha class with data-callback. Example:
<input type="submit" class="g-recaptcha" data-callback="loginFn" onclick="validateForm" value="OK" data-sitekey="XXXXXXXXXXXXXXXXXXXXXXXX">
  • Programmatic definition using grecaptcha.render, where the callback parameter points to the function. Example:
grecaptcha.render("recaptcha", {
    "sitekey": "XXXXXXXXXXXXXXXXXXXXXXXX",
    "callback": verifyCallbackCaptcha,
    "size": "invisible"
})

If the page validates the ReCaptcha (or hCaptcha) via POST, passing the callback_function is usually not necessary.