Skip to content

Instantly share code, notes, and snippets.

@alperensert
Created December 9, 2022 02:12
Show Gist options
  • Save alperensert/f1fecec9629f6a22c489e39337873fe2 to your computer and use it in GitHub Desktop.
Save alperensert/f1fecec9629f6a22c489e39337873fe2 to your computer and use it in GitHub Desktop.
Solve Captchas with Selenium & Capmonster
API_KEY="your_api_key"
WEBSITE_KEY="6Le-wvkSAAAAAPBMRTvw0Q4Muexq9bi0DJwx_mJ-"
WEBSITE_URL="https://google.com/recaptcha/api2/demo"
USER_AGENT="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0"
import os
from time import sleep
from dotenv import load_dotenv
from selenium import webdriver
from selenium.webdriver.common.by import By
from capmonster_python import RecaptchaV2Task
load_dotenv()
API_KEY = os.getenv("API_KEY")
WEBSITE_URL = os.getenv("WEBSITE_URL")
WEBSITE_KEY = os.getenv("WEBSITE_KEY")
USER_AGENT = os.getenv("USER_AGENT")
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
browser = webdriver.Chrome(options=options)
print("Browser is initialized!")
browser.get(WEBSITE_URL)
print("Browser page is switched to captcha page!")
capmonster = RecaptchaV2Task(API_KEY)
print("ReCaptchaV2 solver initialized!")
task_id = capmonster.create_task(WEBSITE_URL, WEBSITE_KEY, no_cache=True)
print("ReCaptchaV2 solving is begin!")
result = capmonster.join_task_result(task_id).get("gRecaptchaResponse")
print("ReCaptchaV2 is solved!")
browser.execute_script("document.getElementsByClassName(`g-recaptcha-response`)[0].innerHTML = " f"'{result}';")
print("Result injected to page!")
browser.find_element(By.ID, "recaptcha-demo-submit").click()
print("Page submitted!")
sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment