Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does JavascriptExecutor works in selenium webdriver

When somebody inserts some javascript in a webpage using selenium's JavascriptExecutor object and executes it, where does this javascript code go and how does it run?

I mean does it sit in the page forever(until the page is reloaded) or it is executed from outside. And if it sits in the page, is it possible to use that code again?

Also, what if some other javascript code is already running in the page then what happens, does inserted code waits for other js code to stop or is executed parallely from outside the browser.

I asked this question because I am working on selenium and using the JavaScriptExecutor. So I just wanted to know how internally it works.

like image 593
me_digvijay Avatar asked Nov 11 '22 19:11

me_digvijay


1 Answers

After looking into all the API and documentation of JavascriptExecutor this is what I have found.

It has two methods namely executeScript and executeAsyncScript. The first methods is synschrous, so if any other already running on the page then possibly the injected script waits for that script to finish.

The other method executes any async script(like ajax call or settimeout functions) so the injected script does not has to wait.

Also the inserted script is executed as a body of an anonymous function. The local variables dimish after script is finished, but global variable do stay in the page and possibly can be reused after the script finishes.

If some has more information on this, please provide. Thank you.

like image 109
me_digvijay Avatar answered Nov 15 '22 03:11

me_digvijay