A
A
AYKostarev2021-04-06 15:09:49
Python
AYKostarev, 2021-04-06 15:09:49

How to wait for response from xmlhttprequest request when executing JS script in Python Selenium?

It is necessary to send a request on the page and return a response (requests is not suitable). I do it through driver.execute_script(). The script looks like this: 'let xhr = new XMLHttpRequest();xhr.open("GET", URL);xhr.send();return xhr.response;'. No response is returned because the request does not have time to complete.
When adding 'xhr.onload = function () { return xhr.response;};' returns None.
Also, I tried to return the value already in the next execute_script, it also does not work.

Can you please tell me how this can be implemented?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
rPman, 2021-04-06
@rPman

In general, replace XMLHttpRequest with your own, which would call your callback on each successful request, something like this

E
Evgeniy _, 2021-04-06
@GeneD88

fails to complete

Does it mean that it takes a long time to complete?
It is necessary to do something at the moment while this request is being executed?
If not, then you can put a wait before your return. Or make it async.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question