Skip to content

Latest commit

 

History

12 Commits

Folders and files

Repository files navigation

Amazon Captcha Solver for Python Selenium (CapMonster Cloud)

tests License: MIT Python 3.9+ Selenium 4

A runnable Python Selenium Amazon captcha solver: it solves the Amazon captcha ("Type the characters you see in this image") in a real Chrome session using the ImageToText task of the official CapMonster Cloud image captcha solver Python SDK. Use it for QA and automation where you are permitted to work with Amazon pages.

What this example does

  1. Opens Amazon in Chrome through Selenium 4; by default the captcha check page amazon.com/errors/validateCaptcha.
  2. Finds the captcha image and the answer input, trying several selectors for different page layouts.
  3. Takes a screenshot of the image element and sends it to CapMonster Cloud as an ImageToTextRequest.
  4. Types the answer, submits the form and repeats if Amazon shows another captcha.

Requirements

  • Python 3.9+
  • Google Chrome (Selenium Manager fetches a matching chromedriver)
  • A CapMonster Cloud API key (see Get an API key)

Quick start

git clone https://github.com/CapMonsterCloud/python-selenium-amazon-captcha-solver.git
cd python-selenium-amazon-captcha-solver
pip install -r requirements.txt

export CAPMONSTER_API_KEY=your_key        # Windows: set CAPMONSTER_API_KEY=your_key
python examples/solve_amazon_captcha_selenium.py --headed
python examples/solve_amazon_captcha_selenium.py --url https://www.amazon.com/dp/B000000000 --attempts 5

Tests (offline):

pytest -q

How it works

  • Element screenshot instead of a second download. image.screenshot_as_png captures exactly the pixels Chrome shows. Downloading the image URL again could return a different captcha or fail without the browser's cookies.
  • Selector fallbacks. Amazon serves several versions of the same page. first_match() walks IMAGE_SELECTORS, INPUT_SELECTORS and SUBMIT_SELECTORS in order and returns the first element found, so one layout change does not stop the script.
  • Real typing. The answer is entered with send_keys, which fires the same keyboard events a person would, and the form is submitted with its own button (or Enter if the button is missing).
  • Answer cleanup and retries. clean_answer() keeps uppercase Latin letters only, and the loop re-checks the page because Amazon may ask again.

ImageToText basics

  • The task takes a picture of the captcha and returns the recognized characters as text.
  • Send only the captcha image itself. The SDK takes the raw image bytes and encodes them for you.
  • Each captcha is solved separately: when Amazon shows a new image, take a new screenshot and send a new request.

Common errors

Symptom Cause and fix
captcha page detected, but the image or input was not found Amazon changed the markup. Inspect the page and add the new selector to IMAGE_SELECTORS / INPUT_SELECTORS.
Empty or black screenshot in headless mode The image was not loaded yet. Wait until image.get_attribute("complete") is true or run with --headed.
captcha is still shown after all attempts The session is flagged. Slow down, keep the same profile and cookies, avoid headless for long runs.
SessionNotCreatedException Chrome and chromedriver versions differ. Remove old drivers from PATH and update Selenium.
[ERROR_ZERO_BALANCE] / [ERROR_KEY_DOES_NOT_EXIST] Top up the balance or check the API key.

Get an API key

This example needs a CapMonster Cloud account and API key. Register and get the key on the image captcha recognition page. There you will also find pricing and the API description. New users can ask support for a $0.10 test balance after registering.

Responsible use

Automated access to Amazon is subject to Amazon's Conditions of Use. Run this example only where you are permitted, for example QA of your own seller workflows, and respect applicable laws. Keep your API key private.

Links


Amazon is a trademark of Amazon.com, Inc. or its affiliates. Selenium is a project of the Selenium community. CapMonster Cloud is not affiliated with or endorsed by these companies.