from requests.api import request from selenium.webdriver.support import expected_conditions as EC from selenium import webdriver import requests import time from selenium.common.exceptions import TimeoutException from selenium.webdriver.common.by import By from selenium.webdriver.support.wait import WebDriverWait pageurl = 'https://visa-fr.tlscontact.com/ma/TNG/login.php' driver = webdriver.Chrome(executable_path=r'chromedriver.exe') driver.get(pageurl) timeout = 4 def isPageLoaded(): try: element_present = EC.presence_of_element_located((By.ID, 'cf-hcaptcha-container')) WebDriverWait(driver, timeout).until(element_present) return 1 except TimeoutException: print("Timed out waiting for page to load") return 0 while isPageLoaded() == 0: isPageLoaded() iframe = driver.find_element_by_tag_name('iframe').get_attribute('src') siteKey = iframe.split("sitekey=")[1].split("&theme=")[0] site_key = siteKey api_key = '9c7b09d4c430edca5828fd8ed009bb0e' form = {"method": "hcaptcha", "sitekey": site_key, "key": api_key, "pageurl": pageurl, "invisible": 1, "json": 1} response = requests.post('http://2captcha.com/in.php', data=form) request_id = response.json()['request'] url = f"http://2captcha.com/res.php?key={api_key}&action=get&id={request_id}&json=1" status = 0 while not status: res = requests.get(url) if res.json()['status'] == 0: print('KO ', res.json()) time.sleep(3) else: requ = res.json()['request'] print('OK ', res.json()) print('token ', requ) #TODO I don't know where to put/submit the token to solve captcha js = f'document.getElementById("h-captcha-response-0o6pidta2t6o").innerHTML="{requ}";' driver.execute_script(js) driver.find_element_by_id("recaptcha-demo-submit").submit() status = 1