Captcha that appears after a while (solution using 2captcha)

103 Views Asked by At

I wrote a script that solves captcha using 2captcha and it works if the captcha is static, such as on https://www.google.com/recaptcha/api2/demo. Now i want to solve a captcha that isn`t immediately visible, but i appear later and a problem arises that an error appears that the captcha was not solved correctly. I no longer have any ideas what the problem could be.

My code:

def solveRecaptcha(site_key, url):
    api_key = os.getenv('APIKEY_2CAPTCHA', 'MY_API_KEY')

    solver = TwoCaptcha(api_key)

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

    except Exception as e:
        print(e)

    else:
        return result
def send_solve():
    browser = uc.Chrome(driver_executable_path="D:\\temp\\chromedriver.exe")
    browser.get('exemple.сom')
    element = (By.ID, 'g-recaptcha-response')
    WebDriverWait(browser, 60).until(
        EC.presence_of_element_located(element)
    )

    
    result = solveRecaptcha(
        "site-key",
        "exemple.сom"
    )
    code = result['code']
    print("solved")

    browser.execute_script("document.getElementById('g-recaptcha-response').innerHTML = " + "'" + code + "'")
    sleep(2)

    # add a button to confirm the captcha solution
    target_div = browser.find_element(By.XPATH,
                                      "/html/body/div[4]/div/div/div[3]/form[2]/div[4]/div/div[1]")
    new_input = browser.execute_script('return arguments[0].appendChild(document.createElement("input"))', target_div)
    browser.execute_script('arguments[0].setAttribute("type", "submit")', new_input)
    browser.execute_script('arguments[0].setAttribute("value", "send")', new_input)
    new_input.send_keys(Keys.RETURN)

I tried my script on other sites with static captcha and it works there.I'm really looking forward to your ideas and comments!

0

There are 0 best solutions below