How can an anticaptcha solver be inserted into selenium code?

Viewed 22

I've made a code to automatize my work, but it requires to solve captchas (hidden recaptcha V2), so I'm using anticaptcha platform. It doesn't works and I don't know why. I barely achieve the first part of my code, I'm very newbie in programming. Any help I will really appreciate. The code looks like this:

op = Options()

op.add_argument('--start-maximized')
op.add_argument('--disable-gpu')
op.add_experimental_option('excludeSwitches', ['enable-logging'])
s = Service('C:\\')
driver = webdriver.Chrome(service=s, options = op)
driver.implicitly_wait(30)

registros = [['1718997925001'],
['2100291042001']

ANTICAPTCHA_KEY = "..."
URL = 'https://srienlinea.sri.gob.ec/sri-en-linea/SriRucWeb/ConsultaRuc/Consultas/consultaRuc'
SITE_KEY = "6LeQLyEUAAAAAKTwLC-xVC0wGDFIqPg1q3Ofam5M"

driver.get(URL)
wait = WebDriverWait(driver, 30)
ids = []
titulo = []
found = False
captcha = False

captcha_result = captcha_obj.captcha_handler(websiteURL=WEB_URL, websiteKey=SITE_KEY)
recaptcha_element.send_keys(captcha_result["solution"]["gRecaptchaResponse"])
time.sleep(2)
post_button = driver.find_element(By.ID, "recaptcha-verify-button")
browser.execute_script("arguments[0].click();", post_button)

for item in registros:
    input = driver.find_element(By.ID, "busquedaRucId")
    input.send_keys(item)
    time.sleep(2)
    driver.find_element(By.XPATH,('/html/body/sri-root/div/div[2]/div/div/sri-consulta-ruc-web-app/div/sri-ruta-ruc/div[2]/div[1]/div[6]/div[2]/div/div[2]/div/button/span[1]')).click()
    try:
        found = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.CLASS_NAME,('border-top-tabla-datos'))))
        captcha = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID,('recaptcha-token'))))
                                                                            
       if found.is_displayed():
          actividad = driver.find_element(By.CLASS_NAME,('border-top-tabla-datos')).text
          estado = driver.find_element(By.XPATH,('/html/body/sri-root/div/div[2]/div/div/sri-consulta-ruc-web-app/div/sri-ruta-ruc/div[2]/div[1]/sri-mostrar-contribuyente/div[1]/div[4]/div/div[1]/div[2]/div/span')).text
          fecha = driver.find_element(By.XPATH,('/html/body/sri-root/div/div[2]/div/div/sri-consulta-ruc-web-app/div/sri-ruta-ruc/div[2]/div[1]/sri-mostrar-contribuyente/div[4]/div/div[5]/div/table/tbody/tr/td[1]')).text

          ids.append(item)
          actividades.append(actividad)
          estados.append(estado)
          fechas.append(fecha)
          time.sleep(1)
          driver.find_element(By.XPATH,('/html/body/sri-root/div/div[2]/div/div/sri-consulta-ruc-web-app/div/sri-ruta-ruc/div[3]/div/div[4]/div/button/span[1]')).click()
        
       elif captcha.is_displayed():
          recaptcha_xpath = '//*[@id="g-recaptcha-response"]'                                                                    
          captcha_obj = NoCaptchaTaskProxyless(anticaptcha_key=ANTICAPTCHA_KEY)
          browser.execute_script("document.getElementById('g-recaptcha-response').style.display = 'block';")
          time.sleep(1)
          recaptcha_element = captcha
          recaptcha_element.click()
          recaptcha_element.clear()                                                                    
          captcha_result = captcha_obj.captcha_handler(websiteURL=WEB_URL, websiteKey=SITE_KEY)
          recaptcha_element.send_keys(captcha_result["solution"]["gRecaptchaResponse"])

          time.sleep(2)
          post_button = browser.find_element_by_xpath(post_xpath)
          browser.execute_script("arguments[0].click();", post_button)
                                                                            
except NoSuchElementException:
    input = driver.find_element(By.ID, ('busquedaRucId')).clear()
0 Answers
Related