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

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

问题描述

Selenium WebDriver 中的 JavaScript Executor 是什么?

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 接口:

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

当您有时执行 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.

你会在 Uncaught 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.

举几个例子:

String js = "arguments[0].setAttribute('value','"+inputText+"')"
((JavascriptExecutor) webDriver).executeScript(js, element);

  • 双击 JavaScript

    new Actions(driver).moveToElement(myElem, posX, posY).perform();
    ((JavascriptExecutor)driver).executeScript(jsDoubleClick, myElem, posX, posY);
    

  • 通过executeScript()发送变量字符串

    String myValue = "80120804076";
    WebElement pesel = driver.findElement(fldPesel);
    jse.executeScript("arguments[0].value='" + myValue + "';", pesel);
    

  • 您还可以在以下位置找到一些关于参数的详细讨论:

    You also can find a couple of detailed discussions about the arguments in:

    跨域策略文件规范

    这篇关于Selenium 中的 JavaScriptExecutor 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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