[英]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? 当有人使用selenium的
JavascriptExecutor
对象在网页中插入一些javascript并执行它时,这个javascript代码在哪里运行?它是如何运行的?
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. 此外,如果其他一些javascript代码已经在页面中运行,那么会发生什么,插入的代码等待其他js代码停止或从浏览器外部并行执行。
I asked this question because I am working on selenium and using the JavaScriptExecutor
. 我问了这个问题,因为我正在研究selenium并使用
JavaScriptExecutor
。 So I just wanted to know how internally it works. 所以我只是想知道它的内部工作原理。
After looking into all the API and documentation of JavascriptExecutor
this is what I have found. 在查看了
JavascriptExecutor
所有API和文档之后,这就是我所发现的。
It has two methods namely executeScript
and executeAsyncScript
. 它有两个方法,即
executeScript
和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. 另一种方法执行任何异步脚本(如ajax调用或settimeout函数),因此注入的脚本不必等待。
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.
谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.