Home > Mobile >  Can not inject token
Can not inject token

Time:07-10

I'm using the 2Captcha API to obtain the token that solves the reCaptcha of a web that validates the criminal record of a person, inserting the ID and the verification code. The thing is that my code works in the reCaptcha demo but not in the page I'm taking about https://sede.mjusticia.gob.es/verificaCSV. Clicking the submit button after injecting the token in the textarea should work, but doesn't (The page reloads and the reCaptha doesn't solved), I don't know what step am I skipping. I would appreciate if there's someone who can identify what part of the web page sends the token to solve the reCaptcha, I have the work delivery this Sunday and no forum or video solves my problem. Thank you!. The code:

import sys
from twocaptcha import TwoCaptcha
from selenium import webdriver
import requests, time
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as ec

#Librerías para completar el uso de WebDriverWait
import time
import pandas as pd

#Death By Captcha
import requests, time

page_url = 'https://sede.mjusticia.gob.es/verificaCSV'
driver = webdriver.Chrome()
driver.get(page_url)

def solve():
    result = None
    sitekey = '6Lf28LAUAAAAACso2fkzeLfJLKN1DBHUrVNpIN1b'
    api_key = 'MY_API_KEY'
    solver = TwoCaptcha(api_key)
    try:
        result = solver.recaptcha(
            sitekey=sitekey,
            url='https://sede.mjusticia.gob.es/verificaCSV'
        )

    except Exception as e:
        sys.exit(e)

    return result

numero_dni = 'ID'
codigo_verificacion = 'VERIFICATION_CODE'

insertarDNI = WebDriverWait(driver, 5)\
                .until(ec.element_to_be_clickable((By.CSS_SELECTOR,"input#documento.inputMediano")))\
                .send_keys(str(numero_dni))

insertarCodigo_Verificacion = WebDriverWait(driver, 5)\
                .until(ec.element_to_be_clickable((By.CSS_SELECTOR,"input#idSolicitud.inputMediano")))\
                .send_keys(str(codigo_verificacion))

textarea = driver.find_element(By.NAME, 'g-recaptcha-response')
solution = solve()
code = solution['code']
driver.execute_script("document.getElementsByName('g-recaptcha-response')[0].style.display = '';")
time.sleep(10)
driver.find_element(By.NAME, 'g-recaptcha-response').send_keys(code)
time.sleep(10)
driver.find_element(By.ID, "submitConsulta").click()
time.sleep(10)

CodePudding user response:

check if any of these work for you:

driver.find_element(By.XPATH, '//button[@]').click()

driver.find_element(By.XPATH, '//button[contains(text(), "Verify")]').click()

driver.execute_script('document.getElementById("recaptcha-verify-button").click;')

or you can also try with ActionChains

from selenium.webdriver.common.action_chains import ActionChains

element = driver.find_element(By.XPATH, '//button[contains(text(), "Verify")]')

action = ActionChains(driver)
action.click(on_element = element)
action.perform()

CodePudding user response:

Thank you for helping me! I tried you codes but there's no button with those characteristics, I tried to submit the form with driver.execute_script("document.getElementById('form_verificacion').submit();") but didn't work. I don't know which part of the HTML code submits the token, if someone can take a look of the web page and identify that part I will be eternally grateful :((.

  • Related