24 days ago

How can the hcaptcha callback function be executed?

I'm currently encountering an issue with the hcaptcha callback function where I'm repeatedly getting the null error (reading 'appendChild'). If anyone has alternative solutions, I would appreciate it. For your reference, I've included a block of code below which involves importing os, threading, time, nopecha, undetected_chromedriver, and tkinter. In order to ensure compatibility with the front end HTML output file, ensure that all code is enclosed within pre tags. Additionally, the error log I'm receiving is shown below:
Exception in thread Thread-2: Traceback (most recent call last):   File "C:\Users\fatih\AppData\Local\Programs\Python\Python39\lib\", line 950, in bootstrapinner   File "C:\Users\fatih\AppData\Local\Programs\Python\Python39\lib\", line 888, in run*self.args, **self.kwargs)   File "C:\Users\fatih\OneDrive\Masaüstü\Python\reklam-project\", line 36, in solvehCaptcha     browser.executescript(jsscript)   File "C:\Users\fatih\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\", line 500, in executescript     return self.execute(command, {"script": script, "args": convertedargs})["value"]   File "C:\Users\fatih\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\", line 440, in execute     self.errorhandler.checkresponse(response)   File "C:\Users\fatih\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\", line 245, in checkresponse     raise exception_class(message, screen, stacktrace) selenium.common.exceptions.JavascriptException: Message: javascript error: Cannot read properties of null (reading 'appendChild')   (Session info: chrome=113.0.5672.64) Stacktrace: Backtrace:    GetHandleVerifier [0x00FE8893+48451]    (No symbol) [0x00F7B8A1]    (No symbol) [0x00E85058]    (No symbol) [0x00E883F1]    (No symbol) [0x00E89DEB]    (No symbol) [0x00EDCDD9]    (No symbol) [0x00ECA2BC]    (No symbol) [0x00EDC482]    (No symbol) [0x00ECA0B6]    (No symbol) [0x00EA7E08]    (No symbol) [0x00EA8F2D]    GetHandleVerifier [0x01248E3A+2540266]  GetHandleVerifier [0x01288959+2801161]  GetHandleVerifier [0x0128295C+2776588]  GetHandleVerifier [0x01072280+612144]   (No symbol) [0x00F84F6C]    (No symbol) [0x00F811D8]    (No symbol) [0x00F812BB]    (No symbol) [0x00F74857]    BaseThreadInitThunk [0x75800099+25]     RtlGetAppContainerNamedObjectPath [0x77917B6E+286]  RtlGetAppContainerNamedObjectPath [0x77917B3E+238]
Lastly, although I'm currently having trouble with the hcaptcha callback function without using a submit button, I'm receiving the error message "Cannot read properties of null (reading 'appendChild')".
22 days ago
Verified Answer
I'm sorry to hear that you're having trouble with the hcaptcha callback function. The error message "Cannot read properties of null (reading 'appendChild')" is usually caused by an issue with the DOM. One possible solution is to ensure that the DOM is fully loaded before executing the script. You can do this by wrapping your code in a document.ready event. Another possible solution is to place your function inside the document.ready event so that it is not in the global scope and therefore unreachable by the captcha control. Here's an example of how you can use the hcaptcha callback function without using a submit button:
import os
import threading
import time
import nopecha
from undetected_chromedriver import Chrome, ChromeOptions
from tkinter import *

def solvehCaptcha():
    options = ChromeOptions()
    browser = Chrome(options=options)
    hcaptcha_element = browser.find_element_by_xpath("//div[@class='h-captcha']")
    hcaptcha_challenge_iframe = browser.find_element_by_xpath("//iframe[@title='hCaptcha challenge']")
    hcaptcha_checkbox = browser.find_element_by_xpath("//div[@class='recaptcha-checkbox-border']")
    js_script = """
        var element = document.getElementById('h-captcha-response');
        var event = new Event('change');
        event.simulated = true;
        element.value = '123456';
root = Tk()

button1 = Button(root, text="Solve hCaptcha", command=solvehCaptcha)

I hope this helps!


Invisible reCAPTCHA | Google DevelopersGoogle reCAPTCHA data-callback not working - Stack OverflowHow to run hcaptcha callback function - Stack OverflowConfiguration | hCaptcha
24 days ago
It looks like you're getting a Cannot read properties of null (reading 'appendChild') error when trying to run the callback function for hcaptcha. This usually means that the element you are trying to append the child to does not exist. In your code, you are trying to append an input element to the captchaElement which you set as document.getElementById('captcha'). You should check to make sure that the captchaElement actually exists on the page before trying to append the input element to it. You can do this by adding a null check before appending the child element, like this:
let captchaElement = document.getElementById('captcha');
if (captchaElement) {
  let inputElement = document.createElement('input');
  inputElement.setAttribute('type', 'hidden');
  inputElement.setAttribute('name', 'response_token');
  inputElement.setAttribute('value', "{token}");
This code first checks if the captchaElement is not null before appending the inputElement to it. This should prevent the error you're seeing.