How to solve invisible captcha on Selenium

1.7k Views Asked by At

Hi I am trying to write a sign-up bot for UEFA.com using Selenium as requests I find to be too difficult for me to try so I am just working on automating the sign-up process even if it is a lot slower.

I am able to get to the final stage where I click on Create an Account, but faced with a reCaptcha which only appears after clicking on Create an Account. And after solving the captcha there is no 'Submit' button but it will automatically submit the details for you.

I am able to get the captcha token returned from 2captcha solving service, and inputted it into the innerHTML of the g-response-token field using javascript. However I do not know how to submit the captcha and the form.

import requests
import time
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from seleniumwire import webdriver
import pyautogui
from twocaptcha import TwoCaptcha
import random
import os
from random import randint
import sys

firstnames = []
lastnames = []

API_Key = '6LehfZUbAAAAAJhue_6BVqqxLulLiXLP0rEgpdRH'

# Open Names File
with open('firstnames.txt', 'r') as f:
    for name in f:
        name = name.strip()
        firstnames.append(name)

with open('lastnames.txt', 'r') as e:
    for name in e:
        name = name.strip()
        lastnames.append(name)

with open('proxies.txt') as f:
    proxy = f.readlines()

proxy_rand = randint(1, 35)
s_proxy = str(proxy[proxy_rand])
p_strip = s_proxy.rstrip()

# Proxy Input and Format
bare_proxy = p_strip.split(':')
username = bare_proxy[2]
password = bare_proxy[3]
ip = bare_proxy[0]
port = bare_proxy[1]

options = {
    'proxy': {
        'http': f'http://{username}:{password}@{ip}:{port}',
        'https': f'https://{username}:{password}@{ip}:{port}',
        'no_proxy': 'localhost,127.0.0.1'
    }
}

os.environ['PATH'] += 'C:/SeleniumDrivers'
homepage_URL = 'https://www.uefa.com/tickets/'

driver = webdriver.Chrome(seleniumwire_options=options)
driver.get(homepage_URL)

# Accessing Register Page
reject_cookies = driver.find_element(By.ID, 'onetrust-reject-all-handler')
reject_cookies.click()
time.sleep(1)

login_button = driver.find_element(By.CSS_SELECTOR, "a[class='btn btn-secondary tickets__btn js-tracking-card']")
login_button.click()
time.sleep(10)

create_account = driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div[2]/div/form/div[4]/a')
create_account.click()
time.sleep(10)

# Inputting SignUp Details

letters = 'abcdefghijklmnopqrstuvwxyz'
a = random.choice(letters)
b = random.choice(letters)
c = random.choice(letters)
d = random.choice(letters)

email = driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div[2]/div/form/div[1]/div[6]/input')
email.send_keys(f'{a}{b}{c}{d}@nottingham.pro')

time.sleep(2)

password = driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div[2]/div/form/div[1]/div[7]/input')
password.send_keys('19741002Rw!')

time.sleep(2)
first_name = driver.find_element(By.XPATH, '//*[@id="gigya-textbox-130722358975432270"]')
first_range = len(firstnames) - 1
random_first = randint(1, first_range)
f_name = firstnames[random_first]
first_name.send_keys(f'{f_name}')

time.sleep(2)
last_name = driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div[2]/div/form/div[1]/div[9]/input')
last_range = len(lastnames) - 1
random_last = randint(1, first_range)
l_name = lastnames[random_last]
last_name.send_keys(f'{l_name}')

time.sleep(2)
day_of_birth = driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div[2]/div/form/div[1]/div[10]/div[1]/input')
day = randint(1, 28)
day_of_birth.send_keys(f'{day}')

time.sleep(2)
month_of_birth = driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div[2]/div/form/div[1]/div[10]/div[2]/input')
month = randint(1, 12)
month_of_birth.send_keys(f'{month}')

time.sleep(2)
year_of_birth = driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div[2]/div/form/div[1]/div[10]/div[3]/input')
year = randint(1940, 2000)
year_of_birth.send_keys(f'{year}')

driver.execute_script("window.scrollTo(0, 500)")
time.sleep(2)

pyautogui.moveTo(353, 619)
time.sleep(2)
pyautogui.click()

time.sleep(5)

current_url = driver.current_url
print(current_url)

g_key = '6LehfZUbAAAAAJhue_6BVqqxLulLiXLP0rEgpdRH'


def SolveCaptcha():
    sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))

    api_key = os.getenv(g_key, 'a733edea49a8327795d56edc9f06d391')

    solver = TwoCaptcha(api_key)

    try:
        result = solver.recaptcha(
            sitekey=g_key,
            url=current_url)

    except Exception as e:
        print(e)

    else:
        return result


result = SolveCaptcha()
code = result['code']
print(code)

token = f'document.getElementById("g-recaptcha-response").innerHTML="{code}";'
driver.execute_script(token)

time.sleep(10000)

As you can see by the end of the code I have managed to input the captcha token but not sure how to submit as there is no submit button

I have tried to look for a callback function but can't seem to find it when I inspect the page.

2

There are 2 best solutions below

0
pguardiario On

submit the first form on the page:

driver.execute_script('document.forms[0].submit()')
0
GlistenSTAR On

I just solve the problem like you. In this problem, it's really important to find a callback function. When you find it correctly and run it, the captcha will be solved.

for example, I run a callback function in my case. my website URL is link

driver.execute_script("grecaptcha.execute();")

I wish you also solve your problem