Skip to content Skip to sidebar Skip to footer

Method Execute_script Don't Wait End Of Script To Return Value With Selenium In Python

i'm trying to make a bot follow for instagram with python and selenium. (I am not a developer javascript or python.) Anyway, i would like return an array of user with js in execute

Solution 1:

The root cause is you call a = setTimeout(loveu, 400); asynchronously, so it returns immediately. I think you need to read how to use setTimeout http://javascript.info/tutorial/settimeout-setinterval.

I have revised your code by calling a = loveu(); instead of a = setTimeout(loveu, 400); and move it into function need_up().

ole = []
script = """
        dd = document.getElementsByClassName("_8mlbc _t5r8b");
        i = 9; ret = []; before = 0;
        function need_up() {
            before = document.getElementsByClassName("_4zhc5 _ook48")[0].title;
            ret.push(before);
            i++;
            a = loveu();
        }
        function loveu() {
            if (i == dd.length){return ret;}
            dd[i].click();
            b = setTimeout(need_up, 5000);
        }
        return loveu();
        """ole = driver.execute_script(script)

Post a Comment for "Method Execute_script Don't Wait End Of Script To Return Value With Selenium In Python"