Error while run the code to bypass a recaptcha

121 Views Asked by At

I'm running the attached code to bypass a recaptcha:

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

from solveRecaptcha import solveRecaptcha

browser = webdriver.Chrome()
browser.get('https://impedimentos.migracion.gob.ec/simiec-consultaImpedimentos/')

result = solveRecaptcha(
    "6Ld38BkUAAAAAPATwit3FXvga1PI6iVTb6zgXw62",
    "https://impedimentos.migracion.gob.ec/simiec-consultaImpedimentos/"
)

code = result['code']

print(code)

WebDriverWait(browser, 10).until(
    EC.presence_of_element_located((By.ID, 'g-recaptcha-response'))
)

browser.execute_script(
    "document.getElementById('g-recaptcha-response').innerHTML = " + "'" + code + "'")

browser.find_element(By.ID, "g-recaptcha-response").click()

time. Sleep(120)

And it displays the following error:

Traceback (most recent call last):
  File "c:\...\webscrapping\captcha.py", line 21, in <module>
    browser.execute_script(
  File "C:\...\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 404, in execute_script
    return self.execute(command, {"script": script, "args": converted_args})["value"]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\...\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 344, in execute
    self.error_handler.check_response(response)
  File "C:\...\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.JavascriptException: Message: javascript error: Cannot set properties of null (setting 'innerHTML')
  (Session info: chrome=117.0.5938.89)

It's important to say that the same code, run perfect in a demo website. I changed the site key obviously and the code run perfect until this line:

`WebDriverWait(browser, 10).until(
    EC.presence_of_element_located((By.ID, 'g-recaptcha-response'))
)`

Help me please

I tried the above code and I expect to bypass the recaptcha. I'm using 2captcha API.

1

There are 1 best solutions below

0
Eb J On

You are attempting to access or manipulate the innerHTML property of an element - document.getElementById('g-recaptcha-response'), but the element you're trying to interact with does not exist in the page's DOM.

I can't access the page from my location to be 100% sure but either one of these could most likely be the reason for this error:

  • The element you're trying to interact with does not exist on the page (as mentioned above)

  • The element is not loaded yet. You may need to wait for the element to become available using WebDriverWait

  • The element might be inside an iframe, shadow DOM, or some other context that you need to switch to before interacting with it. See this

  • By the time you're attempting to access the element, the page's structure or content may have changed, causing the element to become null or no longer present in the DOM