Skip to content

Instantly share code, notes, and snippets.

@alperensert
Last active September 4, 2023 05:09
Show Gist options
  • Save alperensert/ea02f8ec6129bc2289a8ad12d9a76e55 to your computer and use it in GitHub Desktop.
Save alperensert/ea02f8ec6129bc2289a8ad12d9a76e55 to your computer and use it in GitHub Desktop.
How to bypass Google reCaptcha v2 with Python Selenium and Capmonster
from selenium import webdriver
from capmonster_python import RecaptchaV2Task
from time import sleep
class RecaptchaV2Selenium:
def __init__(self, _client_key, executable_path):
self.user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0"
self.captcha = RecaptchaV2Task(_client_key)
self.browser = webdriver.Firefox(executable_path=executable_path)
self.website_url = "https://www.google.com/recaptcha/api2/demo"
def _get_site_key(self):
self.browser.get("https://www.google.com/recaptcha/api2/demo")
return self.browser.find_element_by_id("recaptcha-demo").get_attribute("data-sitekey")
def _solve_recaptcha(self):
self.captcha.set_user_agent(self.user_agent)
task_id = self.captcha.create_task(website_url=self.website_url,
website_key=self._get_site_key(),
no_cache=True)
return self.captcha.join_task_result(task_id=task_id, maximum_time=180).get("gRecaptchaResponse")
def submit_form(self):
self.browser.execute_script("document.getElementsByClassName('g-recaptcha-response')[0].innerHTML = "
f"'{self._solve_recaptcha()}';")
self.browser.find_element_by_id("recaptcha-demo-submit").click()
sleep(10)
self.browser.close()
return self.browser.find_element_by_class_name("recaptcha-success")
if __name__ == "__main__":
client_key = "client_key"
executable_path = "exepath"
recaptcha_selenium = RecaptchaV2Selenium(client_key, executable_path)
@alperensert
Copy link
Author

@icegetec
Just pass the site key manually. Right click to recaptcha box & inspect it. Then find data-sitekey attribute and copy the value.

Before all, your code doesn't compatible with mine. If you really need this, you have to learn more about using Selenium & bypassing/submitting recaptchas.

@alperensert
Copy link
Author

You can get from here

@frozenade
Copy link

frozenade commented Mar 1, 2022

You can get from here

thank you. I got this error:

WebDriverException: Message: Service selenium/chromedriver.exe unexpectedly exited. Status code was: 1

@frozenade
Copy link

Finally I figured it out by using executable_path = "geckodriver.exe". But I got blank page.

@vinifr
Copy link

vinifr commented Dec 19, 2022

Hello, I getting this error:
capmonster_python.utils.CapmonsterException: [ERROR_KEY_DOES_NOT_EXIST] Account authorization key not found in the system or has incorrect format

Is the arg website_url the website itself or google recaptcha url?

URL = https://pocketoption.com/en/login/

<iframe title="reCAPTCHA" src="https://www.google.com/recaptcha/api2/anchor?ar=1&k=6LcU2hgUAAAAAPHmrOrfTDy6DnROjq7Mr8LNwhNF&co=aHR0cHM6Ly9wb2NrZXRvcHRpb24uY29tOjQ0Mw..&hl=pt&type=image&v=5qcenVbrhOy8zihcc2aHOWD4&theme=light&size=normal&cb=a5bmkkozyfou" width="304" height="78" role="presentation" name="a-fxy4vff1wo4t" frameborder="0" scrolling="no" sandbox="allow-forms allow-popups allow-same-origin allow-scripts allow-top-navigation allow-modals allow-popups-to-escape-sandbox"></iframe>

@alperensert
Copy link
Author

Hello @vinifr,

The ERROR_KEY_DOES_NOT_EXIST is means you doesn't have a valid api-key. Did you registered the capmonster.cloud?

And the website_url arg must be https://pocketoption.com/en/login/ in your case. Website URL means where is located the captcha.

@vinifr
Copy link

vinifr commented Dec 19, 2022

ok. Do I have to click in recaptcha checkbox in order to solve it?

driver.find_element(By.CSS_SELECTOR,"iframe[title='reCAPTCHA']").click()

@alperensert
Copy link
Author

No you don't.

Just import your token to g-recaptcha-response

@vinifr
Copy link

vinifr commented Dec 20, 2022

One more question. "driver.find_element(By.ID, "recaptcha-demo-submit").click()"

This is the "Verify" button in recaptcha iframe or the Sign in button ?

@alperensert
Copy link
Author

You don't have to click recaptcha checkbox. As mentioned in name, it's recaptcha's submit button.

@luckylionheart
Copy link

luckylionheart commented Mar 27, 2023

Hello, @alperensert
Currently I can get g-captcha-response and I can write down in text-area.
How can I submit the captcha? In my project, there is no submit button like your's.(self.chrome.find_element(By.ID, "recaptcha-checkbox-checkmark").click())
What is next step?
https://www.tixr.com/groups/beakandskiff/events/rainbow-kitten-surprise-60724
(click vip experience, increase amount and checkout, if you can meet recaptcha in the site)
waiting your kindly reply

@luckylionheart
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment