Selenium中的JavaScriptExecutor是什么? [英] What is JavaScriptExecutor in Selenium?

查看:347
本文介绍了Selenium中的JavaScriptExecutor是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Selenium WebDriver中的JavaScript执行器是什么?

What is JavaScript Executor in Selenium WebDriver?

它的用途是什么,我们如何在Selenium WebDriver中使用它?

What is the use of it and how can we use this in Selenium WebDriver?

一个例子,将不胜感激

推荐答案

JavascriptExecutor

JavascriptExecutor > Selenium 接口,由以下所有类实现:

JavascriptExecutor

JavascriptExecutor is the Selenium interface which is being implemented by all the following classes:

  • FirefoxDriver
  • ChromeDriver
  • InternetExplorerDriver
  • EdgeDriver
  • OperaDriver
  • SafariDriver
  • RemoteWebDriver
  • EventFiringWebDriver
  • FirefoxDriver
  • ChromeDriver
  • InternetExplorerDriver
  • EdgeDriver
  • OperaDriver
  • SafariDriver
  • RemoteWebDriver
  • EventFiringWebDriver

尽管由于跨域策略有时会执行 Selenium 脚本,但浏览器强制执行脚本可能会意外失败,并且没有足够的错误日志记录.当创建自己的XHR请求或尝试访问另一个框架时,这一点尤其重要.

While you execute your Selenium script at times because of cross domain policies browsers enforce your script execution may fail unexpectedly and without adequate error logging. This is particularly pertinent when creating your own XHR request or when trying to access another frame.

您将在未捕获的DOMException中找到详细的讨论. :在页面中列出iframe时,阻止了源为"http://localhost:8080"的框架访问跨域框架

JavascriptExecutor界面提供了以下两种方法:

JavascriptExecutor interface provides two methods as follows:

  • executeScript() :此方法在当前选定的框架或窗口的上下文中执行JavaScript.提供的脚本片段将作为匿名函数的主体执行.在脚本中,您需要使用document来引用当前文档.请注意,脚本完成执行后,局部变量将不可用,尽管全局变量将继续存在.

  • executeScript(): This method executes JavaScript in the context of the currently selected frame or window. The script fragment provided will be executed as the body of an anonymous function. Within the script you need to use document to refer to the current document. Note that local variables will not be available once the script has finished executing, though global variables will persist.

executeAsyncScript() :此方法在当前选定的框架或窗口的上下文中执行异步JavaScript.与执行同步JavaScript不同,使用此方法执行的脚本必须通过调用提供的回调来显式表示已完成脚本.此回调始终作为最后一个参数注入到执行的函数中.

executeAsyncScript(): This method executes an asynchronous piece of JavaScript in the context of the currently selected frame or window. Unlike executing synchronous JavaScript, scripts executed with this method must explicitly signal they are finished by invoking the provided callback. This callback is always injected into the executed function as the last argument.

几个例子:

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆