Caio Lúcio FC
Caio Lúcio FC

Reputation: 11

Anti-Captcha not working, validation happening before callback - Selenium

So, I'm trying to login in this website with Selenium:

https://carrinho.pontofrio.com.br/Checkout?ReturnUrl=%2fSite%2fMeusPedidos.aspx#login

And I'm using anti-captcha, here's my login code:

my_driver = webdriver.Chrome(executable_path=chrome_path)
wait = WebDriverWait(my_driver, 20)


#Realizar o Login
def login():
    my_driver.get(url)
    time.sleep(4)
    my_driver.find_element_by_id('Email').send_keys(usuario)
    my_driver.find_element_by_id('Senha').send_keys(senha)
    my_driver.find_element_by_id('Senha').send_keys(Keys.ENTER)
    time.sleep(1)
    solver = recaptchaV2Proxyless()
    solver.set_verbose(1)
    solver.set_key("")
    solver.set_website_url('https://carrinho.pontofrio.com.br/Checkout?ReturnUrl=%2fSite%2fMeusPedidos.aspx#login')
    solver.set_website_key("6LfeX6kZAAAAAIhuSyQ1XRwZdOS26O-r4UJbW3y1")
    # solver.set_data_s('"data-s" token from Google Search results "protection"')
    g_response = solver.solve_and_return_solution()
    if g_response != 0:
        print("g-response: " + g_response)
    else:
        print("task finished with error " + solver.error_code)
    time.sleep(1)
    my_driver.execute_script('document.getElementById("g-recaptcha-response").innerHTML = "%s"' % g_response)
    time.sleep(1)
    my_driver.execute_script(f"callbackCaptcha('{g_response}');")
login()

Website Key is correct, but the website is not accepting my Captcha responses. So I've tried to check how the Login Process happens with the developer tools, and it goes like that:

The callback function happens after a function that I don't know what it that calls the website: https://www.google.com/recaptcha/api2/userverify?k=6LfeX6kZAAAAAIhuSyQ1XRwZdOS26O-r4UJbW3y1

Post Method before callback method

And I'm not being able to find a way to simulate this post method, since Selenium doesn't do post methods.

Is there anyway that I can listen to all the Javascript events (the codes called) while running the page?

Any help would be much appreciated, thanks!

I was able to solve the validation thing, with the following code:

options.add_argument('--disable-blink-features=AutomationControlled')

But the Anti-Captcha is still giving me a wrong answer :(

Upvotes: 1

Views: 2518

Answers (2)

Crazy Programming
Crazy Programming

Reputation: 267

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))   
driver.execute_script(f"___grecaptcha_cfg.clients[0].O.O.callback('{g_response}')")

Upvotes: 3

Caio Lúcio FC
Caio Lúcio FC

Reputation: 11

So yeah, I've discovered that there were 2 problems, first one being the validation, which I solved with this option:

options.add_argument('--disable-blink-features=AutomationControlled')

And the other one was that the website was generating a new token when I clicked the login button. So I decided to solve the captcha before asking to login, and using the callbackCaptcha to ask for login.

Upvotes: 0

Related Questions