Search code examples
python-3.xseleniumselenium-webdriverrecaptcha2captcha

Selenium to submit recaptcha using 2captcha Python


I am trying to submit Recaptcha on a search form using Python3, Selenium, and 2captcha. Everything is working fine except submitting the Recaptcha after sending google-tokin in the text-area of Recaptcha. Please guide me what am I missing? When I look into my Selenium Webdriver window it shows Recaptcha text-area filled with google-tokin but I am not able to submit it to continue for search result. Thankyou.

from selenium import webdriver
from time import sleep
from datetime import datetime
from twocaptcha import TwoCaptcha
import requests

## Launching webdriver
driverop = webdriver.ChromeOptions()
driverop.add_argument("--start-maximized")
driver = webdriver.Chrome("chromedriver/chromedriver",options=driverop)
url = "https://app.skipgenie.com/Account/Login"
sleep(randint(5,10))
email = "..."
password = ".."

input_data = pd.read_excel("input_data.xlsx")
user_Data = []
driver.get(url)
driver.find_element_by_id("Email").send_keys(email)
driver.find_element_by_id("Password").send_keys(password)
driver.find_element_by_class_name("btn-lg").click()

driver.find_element_by_id("firstName").send_keys(input_data.iloc[0][0])
driver.find_element_by_id("lastName").send_keys(input_data.iloc[0][1])
driver.find_element_by_id("street").send_keys(input_data.iloc[0][2])
driver.find_element_by_id("city").send_keys(input_data.iloc[0][3])
driver.find_element_by_id("state").send_keys(input_data.iloc[0][4])
driver.find_element_by_id("zip").send_keys(int(input_data.iloc[0][5]))

# 2Captcha service
service_key = 'ec.....' # 2captcha service key 
google_site_key = '6LcxZtQZAAAAAA7gY9-aUIEkFTnRdPRob0Dl1k8a' 
pageurl = 'https://app.skipgenie.com/Search/Search' 
url = "http://2captcha.com/in.php?key=" + service_key + "&method=userrecaptcha&googlekey=" + google_site_key + "&pageurl=" + pageurl 
resp = requests.get(url)

if resp.text[0:2] != 'OK': 
    quit('Service error. Error code:' + resp.text) 
captcha_id = resp.text[3:]

fetch_url = "http://2captcha.com/res.php?key="+ service_key + "&action=get&id=" + captcha_id

for i in range(1, 10):  
    sleep(5) # wait 5 sec.
    resp = requests.get(fetch_url)
    if resp.text[0:2] == 'OK':
        break 

driver.execute_script('var element=document.getElementById("g-recaptcha-response"); element.style.display="";')

driver.execute_script("""
  document.getElementById("g-recaptcha-response").innerHTML = arguments[0]
""", resp.text[3:])

Solution

  • Answering the question so the people who encounter situations like this could get help from this answer.

    I was missing that after you get google token you need to display recaptcha text-area and send google-token to text-area like this

    To display text-area of recaptcha.

    driver.execute_script('var element=document.getElementById("g-recaptcha-response"); element.style.display="";')
    

    after that send google token like this:

    driver.execute_script("""
      document.getElementById("g-recaptcha-response").innerHTML = arguments[0]
    """, resp.text[3:])
    

    then you need to make text-area display to none so the search button near repcatcha is clickable.

    driver.execute_script('var element=document.getElementById("g-recaptcha-response"); element.style.display="none";')
    

    then you need to click on the search button to get the search result.