Solve Cloudflare Turnstile Captcha using 2captcha api

1.6k views Asked by At

im having a problem with getting the parameters which 2captcha api needs to solve the captcha . The type of Captcha Im struggeling with, is Cloudflare Turnstile. There are two hidden parameters you need to get which are : *cdata *and *pagedata *. I have read 2captcha blog about Cloudflare Turnstile and how to get the parameters I need through a js code which is this :

const i = setInterval(() => {
                if (window.turnstile) {
                    clearInterval(i);
                    window.turnstile.render = (a, b) => {
                        x = b;
                        return 'foo';
                    };
                }
            }, 50);

. and they have mentioned that I should run this code before the load of the cloudflare widget. But I am having problem with this code i gotta run on that specific time. I can not run this code on the page with Selenium or any other Module or Library that works on browser. How can i run this code on the right time ? preferebly in python. Thanx for helping.

I have tried any type of before load script injection on the page but they did not work. Such as : BroserMob Proxy and etc

2

There are 2 answers

0
Jane Coder On

I ran into this same issue. Try this extension - Custom JavaScript for Websites 2

Here's the js I added to the extension -

function getTurnstileParams(){
    console.log('getTurnstileParams has started');
    if (window.turnstile) {
        clearInterval(i)
        window.turnstile.render = (a,b) => {
        let p = {
            method: "turnstile",
            key: "YOUR API KEY",
            sitekey: b.sitekey,
            pageurl: window.location.href,
            data: b.cData,
            pagedata: b.chlPageData,
            action: b.action,
            json: 1        
        }
        console.log(JSON.stringify(p))
        //window.tsCallback = b.callback
        return 'foo'
        }

    }
}
const i = setInterval(()=>{getTurnstileParams(); },50);
0
cazjonoli On

This is how you could do it with selenium in python.

NOTE : the script snippet is copy pasted from 2captchas site

driver.execute_script('''
const i = setInterval(()=>
{
    if (window.turnstile)
    {
        clearInterval(i)
        window.turnstile.render = (a,b) =>
        {
            let p =
            {
                method: "turnstile",
                key: "YOUR_API_KEY",
                sitekey: b.sitekey,
                pageurl: window.location.href,
                data: b.cData,
                pagedata: b.chlPageData,
                action: b.action,
                userAgent: navigator.userAgent,
                json: 1
            }
            console.log(JSON.stringify(p))
            window.tsCallback = b.callback
            return 'foo'
        }
    }
}, 50)
''')