0
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
3
  • I think if you use raw Selenium Explicit waits, you would be able to solve this issue. 3rd point from your question Commented Sep 7, 2021 at 12:59
  • You can do it using async / await. Commented Sep 7, 2021 at 13:00
  • @Invizi can you explain please Commented Sep 7, 2021 at 13:07

1 Answer 1

1

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("https://example.com")
    return response.text

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

Sign up to request clarification or add additional context in comments.

2 Comments

Still doesn't work, the code I tried is here: codeshare.io/K8gZOo
By the looks of the selenium docs, you want to swap .execute_script with .execute_async_script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.