Back

Technologies:

javascriptjavascript
avatar
Tolerim
2 hours ago

While utilizing JS in the Chrome console, I am encountering an error when attempting to delete the DNS records from Cloudflare.

The provided code block is a JavaScript function that automates the deletion of a record by clicking on a specific set of buttons on a webpage. However, it seems that the function is currently throwing an error with the message "Uncaught TypeError: tabs[0].firstChild.click is not a function". It is likely that the error has to do with the fact that the "click" method is being called on the wrong element or that the elements being targeted do not exist on the page. Further troubleshooting is required to resolve this issue.
Answers(1)
avatar
Tolerim
2 hours ago
Verified Answer
The error "Uncaught TypeError: tabs[0].firstChild.click is not a function" means that the click method is not available on the firstChild property of the element at index 0 of the tabs NodeList. To fix this error, we should check if the firstChild of the element at index 0 is actually a button with the click method available. This can be done by using the instanceof operator to check if the firstChild is an instance of the HTMLButtonElement interface before calling its click method. Here's an example of how we can modify the code to fix the error:
(() => {
    const deleteTopRecord = () => {
        document.querySelector('.c_oj.c_ok.c_z.c_ol.c_om.c_it.c_r.c_on.c_oo.c_dt.c_hl.c_bi.c_op.c_ac.c_al.c_oq.c_or.c_os.c_ot.c_ou.c_ov.c_as.c_eo.c_at').click();
        setTimeout(() => {
            var tabs = document.querySelectorAll('button.c_acg.c_hy.c_ach.c_ia.c_aci.c_ic.c_acj.c_ie.c_if.c_ig.c_ih.c_ack.c_ij.c_z.c_r.c_s.c_hk.c_dt.c_dn.c_ik.c_il.c_im.c_in.c_io.c_ip.c_iq.c_m.c_t.c_ir.c_is.c_it.c_au.c_iu.c_iv.c_ak.c_gm.c_bd.c_dp.c_ck.c_bp.c_bl.c_hu.c_hv.c_acl.c_iy.c_eo.c_iz.c_ja');
            if (tabs.length) {
                const firstTab = tabs[0];
                if (firstTab.firstChild instanceof HTMLButtonElement) {
                    firstTab.firstChild.click();
                    setTimeout(() => {
                        document.querySelector('button.c_acg.c_hy.c_ach.c_ia.c_aci.c_ic.c_acj.c_ie.c_if.c_ig.c_ih.c_ack.c_ij.c_z.c_r.c_s.c_hk.c_dt.c_dn.c_ik.c_il.c_im.c_in.c_io.c_ip.c_iq.c_m.c_t.c_ir.c_is.c_it.c_au.c_iu.c_iv.c_ak.c_gm.c_dj.c_dp.c_bc.c_bp.c_bl.c_hu.c_hv.c_acl.c_iy.c_eo.c_iz.c_ja').click();
                    }, 200);
                    setTimeout(deleteTopRecord, 500);
                } else {
                    setTimeout(deleteTopRecord, 100);
                }
            } else {
                setTimeout(deleteTopRecord, 100);
            }
        }, 500);
    };
    deleteTopRecord();
})();
In this modified code, we assign the element at index 0 of the tabs NodeList to a new constant firstTab. We then check if its firstChild property is an instance of HTMLButtonElement before calling its click method. This should fix the error.
;