selenium webdriver 执行javascript代码 executeScript方法和executeAsyncScript方法
来源:互联网 发布:伸手党福利 优化 编辑:程序博客网 时间:2024/06/04 23:59
Execute an asynchronous piece of JavaScript in the context of the currently selected frame or window.
在当前选定的框架或窗口中的上下文中执行一个异步的JavaScript。
java.lang.Object executeScript(java.lang.String script, java.lang.Object... args)
Executes JavaScript in the context of the currently selected frame or window.
在当前选定的框架或窗口中的上下文中执行JavaScript。
executeScript
java.lang.Object executeScript(java.lang.String script,
java.lang.Object... args)
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, 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.
If the script has a return value (i.e. if the script contains a return statement), then the following steps will be taken:
For an HTML element, this method returns a WebElement
For a decimal, a Double is returned
For a non-decimal number, a Long is returned
For a boolean, a Boolean is returned
For all other cases, a String is returned.
For an array, return a List<Object> with each object following the rules above. We support nested lists.
Unless the value is null or there is no return value, in which null is returned
Arguments must be a number, a boolean, a String, WebElement, or a List of any combination of the above. An exception will be thrown if the arguments do not meet these criteria. The arguments will be made available to the JavaScript via the "arguments" magic variable, as if the function were called via "Function.apply"
Parameters:
script - The JavaScript to execute
args - The arguments to the script. May be empty
Returns:
One of Boolean, Long, String, List or WebElement. Or null.
===============================================================================================
executeAsyncScript
java.lang.Object executeAsyncScript(java.lang.String script,
java.lang.Object... args)
Execute 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.
The first argument passed to the callback function will be used as the script's result. This value will be handled as follows:
For an HTML element, this method returns a WebElement
For a number, a Long is returned
For a boolean, a Boolean is returned
For all other cases, a String is returned.
For an array, return a List<Object> with each object following the rules above. We support nested lists.
Unless the value is null or there is no return value, in which null is returned
Example #1: Performing a sleep in the browser under test.
long start = System.currentTimeMillis();
((JavascriptExecutor) driver).executeAsyncScript(
"window.setTimeout(arguments[arguments.length - 1], 500);");
System.out.println(
"Elapsed time: " + System.currentTimeMillis() - start);
Example #2: Synchronizing a test with an AJAX application:
WebElement composeButton = driver.findElement(By.id("compose-button"));
composeButton.click();
((JavascriptExecutor) driver).executeAsyncScript(
"var callback = arguments[arguments.length - 1];" +
"mailClient.getComposeWindowWidget().onload(callback);");
driver.switchTo().frame("composeWidget");
driver.findElement(By.id("to")).sendKeys("bog@example.com");
Example #3: Injecting a XMLHttpRequest and waiting for the result:
Object response = ((JavascriptExecutor) driver).executeAsyncScript(
"var callback = arguments[arguments.length - 1];" +
"var xhr = new XMLHttpRequest();" +
"xhr.open('GET', '/resource/data.json', true);" +
"xhr.onreadystatechange = function() {" +
" if (xhr.readyState == 4) {" +
" callback(xhr.responseText);" +
" }" +
"}" +
"xhr.send();");
JSONObject json = new JSONObject((String) response);
assertEquals("cheese", json.getString("food"));
Script arguments must be a number, a boolean, a String, WebElement, or a List of any combination of the above. An exception will be thrown if the arguments do not meet these criteria. The arguments will be made available to the JavaScript via the "arguments" variable.
Parameters:
script - The JavaScript to execute.
args - The arguments to the script. May be empty.
Returns:
One of Boolean, Long, String, List, WebElement, or null.
- selenium webdriver 执行javascript代码 executeScript方法和executeAsyncScript方法
- selenium Webdriver 封装方法
- selenium webdriver 常用方法
- selenium webdriver定位方法
- selenium webdriver 元素定位方法
- selenium webdriver重写的方法
- Selenium Webdriver元素定位方法
- Selenium不同浏览器WebDriver处理方法
- Selenium + Webdriver 学习(四) 元素定位方法
- Selenium + Webdriver 学习(四) 元素定位方法
- Selenium webdriver 元素定位方法总结
- webdriver+selenium+junit,打开不同浏览器方法
- selenium webdriver方法qiut()close()区别
- Selenium WebDriver API常用方法解释
- 【学习笔记】Selenium WebDriver的Actions类中的sendKeys方法和WebElement.sendKeys方法的区别
- selenium webdriver学习(十六)----------用selenium webdriver实现selenium RC中的类似的方法
- selenium webdriver学习(十六)----------用selenium webdriver实现selenium RC中的类似的方法
- javascript--执行字符串代码和变量的几种方法
- ffmpeg neon优化必看!!android下编译ffmpeg with neon的正确方法(已验证)
- IO:重定向标准输入流
- 1050. String Subtraction (20)
- 系统分区不求人 Windows7分区功能详解!
- C语言运算符优先级 详细列表
- selenium webdriver 执行javascript代码 executeScript方法和executeAsyncScript方法
- 自定义StartIO
- svn:E205007的解决方案
- Solr4.4部署在tomcat6中
- C++数据结构之最小生成树
- socket 编程入门教程(一)TCP server 端:4、构造函数涉及的概念
- 分析elf文件的利器(常用命令)
- OpenCV 2.4.6 + VS2010 + win7 环境配置
- 聚簇索引与非聚簇索引的区别以及SQL Server查询优化技术