How to pass returned value from javascript async function to python variable

var = driver.execute_script("setTimeout(function(){ return [1,2,3]; }, 1000);")

Using Selenium‘s function execute_script, I’m trying to get data out of a website using javascript and pass it to a python variable var, the problem is that the javascript code that collects the data takes around 1 second to complete collecting the data (because of some animations), so when the line of code above is executed, the right value at that time is None (because the javascript code didn’t return anything), but, 1 second later the right value will be changed to the data needed, still var will be None because that’s the value assigned to it.

Note that this line of code works well, but that’s not exactly what needed.

var = driver.execute_script("return [1,2,3];")

I have solutions to the problem but I’m looking for better ones:

  1. let the javascript code download the data to a file, and after time.sleep for the python code, let var get the data from that file
  2. similar to 1, let the javascript code push the data to somewhere in the website, and after time.sleep for the python code, let var get the data from the website.
  3. just use raw Selenium code, which I don’t prefer because I don’t know other than the basics of Selenium


There is a python library asyncio that lets you use async await in python.

import asyncio
import request

async def do_task():
    response = await request("")
    return response.text

as you can see above, we can use await to tell python to wait for the value before continuing.