How to bypass reCaptcha V3 with Selenium Python?
I am writing this article on 19/4/2021.
We will be using python Selenium library to bypass google reCaptcha v3. Follow the step by step method to get the results.
For the demo purpose, we will be using Google reCaptcha api demo.
Link:
First, you need to Disable Protected content setting of your Chrome browser.
Lets do it, Got to the Setting in Chrome. And write “site settings” in the search bar.
Move into the Site settings. And search for the “Protected content”.
Move into the protected content and disable it.
Now, moving towards the coding part.
we will be using Python 3 in this article. And two libraries will be used. If you want to setup for Selenium and want to know how to set it up. Move to this article: https://medium.com/@mrabdulbasit1999/selenium-with-python-web-automation-f85dfa2e58fa
Let’s move forward,
Install Beautiful Soup library for the script.
pip install beautifulsoup4
Open the script file and import the mentioned libraries into the script.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from http_request_randomizer.requests.proxy.requestProxy
import RequestProxy
import os, sys
import time,requests
from bs4 import BeautifulSoup
Set “delayTime” and “audioToTextDelay” according to your internet speed. Pretty much set values work for all.
delayTime = 2
audioToTextDelay = 10
byPassUrl is the URL which you want to target. And option is used to select chrome driver and some arguments are passed to it.
filename = ‘1.mp3’
byPassUrl = ‘https://www.google.com/recaptcha/api2/demo'
googleIBMLink = ‘https://speech-to-text-demo.ng.bluemix.net/'option = webdriver.ChromeOptions()
option.add_argument('--disable-notifications')
option.add_argument("--mute-audio")
The rest of the code is given in below. Now I will explain how it works.
When the script runs, it checks the I’m not a robot field.
And this pops up (as usual)
After that script selects the audio button at bottom left.
And this shows up. After that script downloads the audio. into the same director with the name of “1.mp3”.
it takes few seconds don’t worry. After that new tab is opened in to the browser which goes to the watson speech to text converter and uploads the file.
And the audio file is converted into text as you can see. It copies the text and paste it into textfield.
And presses the verify button.
Here you go… Problem Solved. If you have any queries or problems shoot it out. I will answer them as soon as possible.
Code
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from http_request_randomizer.requests.proxy.requestProxy
import RequestProxy
import os, sys
import time,requests
from bs4 import BeautifulSoupdelayTime = 2
audioToTextDelay = 10
filename = '1.mp3'
byPassUrl = 'https://www.google.com/recaptcha/api2/demo'
googleIBMLink = 'https://speech-to-text-demo.ng.bluemix.net/'option = webdriver.ChromeOptions()
option.add_argument('--disable-notifications')
option.add_argument("--mute-audio")
# option.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
option.add_argument("user-agent=Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1")def audioToText(mp3Path):
print("1")
driver.execute_script('''window.open("","_blank");''')
driver.switch_to.window(driver.window_handles[1])
print("2")
driver.get(googleIBMLink)
delayTime = 10
# Upload file
time.sleep(1)
print("3")
# Upload file
time.sleep(1)
root = driver.find_element_by_id('root').find_elements_by_class_name('dropzone _container _container_large')
btn = driver.find_element(By.XPATH, '//*[@id="root"]/div/input')
btn.send_keys('C:/Users/AbdulBasit/Documents/google-captcha-bypass/1.mp3')
# Audio to text is processing
time.sleep(delayTime)
#btn.send_keys(path)
print("4")
# Audio to text is processing
time.sleep(audioToTextDelay)
print("5")
text = driver.find_element(By.XPATH, '//*[@id="root"]/div/div[7]/div/div/div').find_elements_by_tag_name('span')
print("5.1")
result = " ".join( [ each.text for each in text ] )
print("6")
driver.close()
driver.switch_to.window(driver.window_handles[0])
print("7")
return resultdef saveFile(content,filename):
with open(filename, "wb") as handle:
for data in content.iter_content():
handle.write(data)driver = webdriver.Chrome(ChromeDriverManager().install(), options=option)
driver.get(byPassUrl)
time.sleep(1)
googleClass = driver.find_elements_by_class_name('g-recaptcha')[0]
time.sleep(2)
outeriframe = googleClass.find_element_by_tag_name('iframe')
time.sleep(1)
outeriframe.click()
time.sleep(2)
allIframesLen = driver.find_elements_by_tag_name('iframe')
time.sleep(1)
audioBtnFound = False
audioBtnIndex = -1for index in range(len(allIframesLen)):
driver.switch_to.default_content()
iframe = driver.find_elements_by_tag_name('iframe')[index]
driver.switch_to.frame(iframe)
driver.implicitly_wait(delayTime)
try:
audioBtn = driver.find_element_by_id('recaptcha-audio-button') or driver.find_element_by_id('recaptcha-anchor')
audioBtn.click()
audioBtnFound = True
audioBtnIndex = index
break
except Exception as e:
passif audioBtnFound:
try:
while True:
href = driver.find_element_by_id('audio-source').get_attribute('src')
response = requests.get(href, stream=True)
saveFile(response,filename)
response = audioToText(os.getcwd() + '/' + filename)
print(response)driver.switch_to.default_content()
iframe = driver.find_elements_by_tag_name('iframe')[audioBtnIndex]
driver.switch_to.frame(iframe)inputbtn = driver.find_element_by_id('audio-response')
inputbtn.send_keys(response)
inputbtn.send_keys(Keys.ENTER)time.sleep(2)
errorMsg = driver.find_elements_by_class_name('rc-audiochallenge-error-message')[0]if errorMsg.text == "" or errorMsg.value_of_css_property('display') == 'none':
print("Success")
breakexcept Exception as e:
print(e)
print('Caught. Need to change proxy now')
else:
print('Button not found. This should not happen.')
Thank you!