Home > other >  Can't obtain Captcha's id from this site (using python)
Can't obtain Captcha's id from this site (using python)

Time:03-23

My problem is that I have a captcha value inside an HTML element which I can't seem to be able to capture using the find_element() function.

I have this piece of code

iframe = driver.find_element_by_xpath("//iframe\[@name='reCAPTCHA'\]")
driver.switch_to.frame(iframe)

ID_token   = driver.find_element(By.ID,"recaptcha-token").get_attribute('value')
print(ID_token)

Whose purpose is to get the value out of the hidden element containing the captcha tokenCaptchaID But the code simply dies in these lines without telling me what went wrong

This is the link for the page where the captcha is.

Would appreciate the help.

CodePudding user response:

You need to first switch to the below frame:

//iframe[@title='reCAPTCHA']

and then us execute_script to inject JS code to get the hidden value.

Code:

driver.maximize_window()


driver.get("https://cadprev.previdencia.gov.br/Cadprev/pages/modulos/dair/consultarDemonstrativos.xhtml")
wait = WebDriverWait(driver, 30)

wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[@title='reCAPTCHA']")))
print('Switched to frame')

script = "return document.getElementById('recaptcha-token').getAttribute('value');";
print(driver.execute_script(script))

Imports:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Output:

Switched to frame
03AGdBq25p6cmSsoDCWUmTX10Z9OyHR3_58_te8n14eXM0VAS8Tdf1LwLit7us0gf3u3c0hYJMSB1m-2a8lNHBMLrOqlKWQECCLopsUwiSmivTkhb-SRmBcHxNrPQdO5DWBEL0LGHCUHCZ4XJfzswQ_hWNp8PdwZLuTaQRZ1l1ecGiZIMbapxSaz0GPrwfbJfbaXIGZFMsDP-7ofnAkhIir_CxULKbOrj8WyJIct4v--aINIiizn2b0lRiTUuqZZAzDmNKysWDfIRXVa2N4jcZq_Hc9FBSx8zmIyMzxhHDxAxrDYXYrIh1GyBx90RDac3iqKi17g4x_c8ohSUicjpUI41fGyHabZAco-fJd6bgCyM-V1itIZmLHZKSePMhM87DrkWA_xf1_RBmNSDnDzhXCUUXLy0iPfNfRBGInVQfnTBcyA0qxvkv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