How can I bypass the Google CAPTCHA with Selenium and Python?

Bypass as in solve it or bypass as in never get it at all?

To solve it:

  • sign up with 2captcha, capmonster cloud, deathbycaptcha, etc. and follow their instructions. They will give you a token that you pass with the form.

To never get it at all:

  • Make sure you have good IP reputation (most important for Cloudflare).
  • Make sure you have a good browser fingerprint (most important for Distil) - I recommend puppeteer + the stealth plugin.

Clear Browsing History, cached data, cookies and other site data First Create an Google Account while you are in browser window opened by selenium. Sign in to your account

wd.get("https://accounts.google.com/signin/v2/identifier?hl=en&passive=true&continue=https%3A%2F%2Fwww.google.com%2F%3Fgws_rd%3Dssl&ec=GAZAmgQ&flowName=GlifWebSignIn&flowEntry=ServiceLogin");
    Thread.sleep(2000);
    wd.findElement(By.name("identifier")).sendKeys("Email"+Keys.ENTER);
    Thread.sleep(3000);
    wd.findElement(By.name("password")).sendKeys("Password"+Keys.ENTER);
    Thread.sleep(5000);

Then Open any website that uses recaptcha tick on checkmark using this code

String framename=wd.findElement(By.tagName("iframe")).getAttribute("name");
            wd.switchTo().frame(framename);
    wd.findElement(By.xpath("//span[@id='recaptcha-anchor']")).click();

You won't find any Puzzles or anything.


To start with using Selenium's Python clients, you should avoid solving/bypass Google CAPTCHA.


Selenium

Selenium automates browsers. Now, what you want to achieve with that power is entirely up to individuals, but primarily it is for automating web applications through browser clients for testing purposes and of coarse it is certainly not limited to that.


CAPTCHA

On the other hand, CAPTCHA (the acronym being ...Completely Automated Public Turing test to tell Computers and Humans Apart...) is a type of challenge–response test used in computing to determine if the user is human.

So, Selenium and CAPTCHA serves two completely different purposes and ideally shouldn't be used to achieve any interrelated tasks.

Having said that, reCAPTCHA can easily detect the network traffic and identify your program as a Selenium driven bot.


Generic Solution

However, there are some generic approaches to avoid getting detected while web scraping:

  • The first and foremost attribute a website can determine your script/program by is through your monitor size. So it is recommended not to use the conventional Viewport.
  • If you need to send multiple requests to a website, keep on changing the User Agent on each request. Here you can find a detailed discussion on Way to change Google Chrome user agent in Selenium?
  • To simulate humanlike behavior, you may require to slow down the script execution even beyond WebDriverWait and expected_conditions inducing time.sleep(secs). Here you can find a detailed discussion on How to sleep Selenium WebDriver in Python for milliseconds

This use case

However, in a couple of use cases we were able to interact with the reCAPTCHA using Selenium and you can find more details in the following discussions:

  • How to click on the reCAPTCHA using Selenium and Java
  • CSS selector for reCAPTCHA checkbok using Selenium and VBA Excel
  • Find the reCAPTCHA element and click on it — Python + Selenium

References

You can find a couple of related discussion in:

  • How can I make a Selenium script undetectable using GeckoDriver and Firefox through Python?
  • Is there a version of Selenium WebDriver that is not detectable?

tl; dr

  • How does reCAPTCHA 3 know I'm using Selenium/chromedriver?

In order to bypass the CAPTCHA when scraping Google, you have to manually solve a CAPTCHA and export the cookies Google gives you. Now, every time you open a Selenium WebDriver, make sure you add the cookies you exported. The GOOGLE_ABUSE_EXEMPTION cookie is the one you're looking for, but I would save all cookies just to be on the safe side.

If you want an additional layer of stability in your scrapes, you should export several cookies and have your script randomly select one of them each time you ping Google.

These cookies have a long expiration date so you wouldn't need to get new cookies every day.

For help on saving and loading cookies in Python and Selenium, you should check out this answer: How to save and load cookies using Python + Selenium WebDriver