I have a website I want to crawl. To access the search results, you must first solve a Recaptcha V2 with a callback function (see screenshot below)
Recaptcha V2 with a callback function
I am using a dedicated captcha solver called 2captcha. The service provides me with a token, which I then plug into the callback function to bypass the captcha. I found the callback function using the code in this GitHub Gist and I am able to invoke the function successfully in the Console of Chrome Dev Tools
The function can be invoked by typing any of these two commands
window[___grecaptcha_cfg.clients[0].o.o.callback]('captcha_token')
or
verifyAkReCaptcha('captcha_token')
However, when I invoke these functions using the driver.execute_script()
method in Python Selenium, I get an error. I also tried executing **other standard Javascript functions **with this method (e.g., scrolling down a page), and I keep getting errors. It's likely because the domain I am trying to crawl prevents me from executing any Javascript with automation tools.
So, my question is, how can I invoke the callback function after I obtain the token from the 2captcha service? Would appreciate all the help I could get. Thank you in advance to hero(in) who will know his/her way around this tough captcha. Cheers!!
Some extra info to help with my question:
Automation framework used --> Python Selenium or scrapy. Both are fine by me
Error messages --> Error message 1 and Error message 2
Code
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from twocaptcha import TwoCaptcha
from dotenv import load_dotenv
import os
# Load environment variables
load_dotenv()
# Instantiate a solver object
solver = TwoCaptcha(os.getenv("CAPTCHA_API_KEY"))
sitekey = "6Lfwdy4UAAAAAGDE3YfNHIT98j8R1BW1yIn7j8Ka"
url = "https://suchen.mobile.de/fahrzeuge/search.html?dam=0&isSearchRequest=true&ms=8600%3B51%3B%3B&ref=quickSearch&sb=rel&vc=Car"
# Set chrome options
chrome_options = Options()
chrome_options.add_argument('start-maximized') # Required for a maximized Viewport
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging', 'enable-automation'])
chrome_options.add_experimental_option("detach", True)
chrome_options.add_experimental_option('prefs', {'intl.accept_languages': 'en,en_US'})
# Instantiate a browser object and navigate to the URL
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get(url)
driver.maximize_window()
def solve(sitekey, url):
try:
result = solver.recaptcha(sitekey=sitekey, url=url)
except Exception as e:
exit(e)
return result.get('code')
captcha_key = solve(sitekey=sitekey, url=url)
print(captcha_key)
# driver.execute_script(f"window[___grecaptcha_cfg.clients[0].o.o.callback]('{captcha_key}')") # This step fails in Python but runs successfully in the console
# driver.execute_script(f"verifyAkReCaptcha('{captcha_key}')") # This step fails in Python but runs successfully in the console