1

I'm trying to solve an invisible reCaptcha with an API, but I don't know how can I execute the callback function and send the token with selenium.

Below is the snippet of my code

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
from anticaptchaofficial.recaptchav2proxyless import *


def solve_recaptcha(site):
    

    solver = recaptchaV2Proxyless()
    solver.set_verbose(1)
    solver.set_key("mykey")
    solver.set_website_url(site)
    solver.set_website_key("6LcmDCcUAAAAAL5QmnMvDFnfPTP4iCUYRk2MwC0-")
    
    
    g_response = solver.solve_and_return_solution()
    if g_response != 0:
        return g_response
    else:
        return 0


site = 'https://recaptcha-demo.appspot.com/recaptcha-v2-invisible.php'
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=options,executable_path='chromedriver.exe')    
driver.get(site)

g_recaptcha = solve_recaptcha(site)
driver.execute_async_script("var token = '{}'; onSubmit(token);", g_recaptcha)
WebDriverWait(driver, 2).until(EC.visibility_of_element_located((By.XPATH, "//button[contains(text(), 'Submit ↦')]"))).click()

And this is the function:

        function onSubmit(token) {
            document.getElementById("demo-form").submit();
        }
    

I receive this message from the site:

Note: Error code missing-input-response may mean the user just didn't complete the reCAPTCHA.

So, I'm not sure, but probably the the webdriver is not sending the var. And I don't know how should I proceed.

5
  • That function doesn't do anything with the token. You need to find the textarea and set it's innerText, and then submit the form. Commented Oct 22, 2020 at 5:04
  • I've tryied to send the token in the g-recaptcha-response , with this command: driver.execute_script("""document.getElementById("g-recaptcha-response").innerHTML = arguments[0]""", g_recaptcha),and click on submit. But it doesn't work too. Commented Oct 22, 2020 at 12:31
  • Hmm, that looks right to me actually. Make sure the captcha is right and there's no callback. Commented Oct 22, 2020 at 13:17
  • There is callback actually. Commented Oct 22, 2020 at 13:24
  • You should update your question if the code has changed Commented Oct 23, 2020 at 0:25

1 Answer 1

1

I solved the problem

I've finally managed to resolve this myself. In case anyone else is struggling with a similar issue, here was my solution:

  • Open the console and execute the following cmd: ___grecaptcha_cfg.clients
  • Find the path which has the callback function, in my case it's ___grecaptcha_cfg.clients[0].O.O
  • Use the following code: driver.execute_script(f"___grecaptcha_cfg.clients[0].O.O.callback('{new_token}')") (Remember to change the path accordingly)

This Article will help you to find the ___grecaptcha_cfg.clients of your recaptcha site

driver.execute_script('document.getElementById("g-recaptcha-response").innerHTML = "{}";'.format(g_response))   
time.sleep(3)  
driver.execute_script(f"___grecaptcha_cfg.clients[0].O.O.callback('{g_response}')")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.