ad-m / python-anticaptcha

Client library for solve captchas with Anticaptcha.com support.
http://python-anticaptcha.readthedocs.io/en/latest/
MIT License
219 stars 51 forks source link

Unable to submit new captcha token #82

Closed GoldDust69 closed 3 years ago

GoldDust69 commented 3 years ago

Good day, I have the following script written and everything seems to work okay up until it needs to submit the new captcha token where it fails to find the element. I've tried several ways but cannot seem to find the correct. I would be ever so grateful if someone could kindly help me. Here is the code:

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from python_anticaptcha import AnticaptchaClient, NoCaptchaTaskProxylessTask

def get_solution(current_url):
    api_key = 'REDACTED'
    site_key = '6LeJGvwUAAAAACRrkKN1AmgqHPxLNh1YMGraggyA'
    client = AnticaptchaClient(api_key)
    task = NoCaptchaTaskProxylessTask(current_url, site_key)
    job = client.createTask(task)
    print("Waiting for solution by Anticaptcha workers") 
    job.join()
    response = job.get_solution_response()
    print("Received solution", response)
    return response

def checkout_settings():
    options = webdriver.ChromeOptions()
    options.add_argument("--window-size=1920,1080")
    options.add_argument("--start-maximized")
    options.add_argument('disable-gpu')
    driver = webdriver.Chrome(executable_path='C:\\Users\\REDACTED\\Desktop\\Discord\\chromedriver', options=options)
    return driver

def auto_login():
    driver = checkout_settings()
    driver.get('REDACTED')
    time.sleep(5)
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="signIn"]'))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="user"]'))).send_keys('email')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="pass"]'))).send_keys('pass')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="loginLink"]'))).click()
    time.sleep(5)
    response = get_solution(driver.current_url)
    time.sleep(5)
    driver.execute_script('document.getElementById("g-recaptcha-response").innerHTML = "%s"' % response)
    time.sleep(5)
    driver.find_element_by_xpath('//*[@id="recaptcha-verify-button"]').submit() ##<-- ISSUE
    time.sleep(5)
    driver.quit()

if __name__ == '__main__':
    auto_login()

UPDATE: After reading #77 I even tried changing the 2 lines to this:

    driver.execute_script("document.getElementById('g-recaptcha-response').innerHTML='{}';".format(response))
    time.sleep(5)
    driver.execute_script("grecaptcha.recaptchaCallback[0]('{}')".format(response))

Sadly still no luck, I'm guessing I somehow need to interact with the callback but unsure how. Here is the error I received:

    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.JavascriptException: Message: javascript error: Cannot read property '0' of undefined
  (Session info: chrome=85.0.4183.102)

UPDATE2: Just to clarify in case someone does get chance to look at this and wants to replicate. The recaptcha happens for me when you login (you can have a free account) and only when using this automation.

GoldDust69 commented 3 years ago

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

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

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

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

When I put to print only the _grecaptchacfg it appears "none", how do I print the value of grecaptcha_cfg with driver.execute_script?

FuckingToasters commented 2 years ago

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

1. Open the console and execute the following cmd: __grecaptcha_cfg.clients

2. Find the path which has the callback function, in my case it's ___grecaptcha_cfg.clients[0].O.O

3. Use the following code: driver.execute_script(f"___grecaptcha_cfg.clients[0].O.O.callback('{new_token}')")
   (Remember to change the path accordingly)

sadly this isn't the solution. upon executing the command you'll somehow get a undefined error. (the site use hcaptcha)