Selenium Webdriver 中的 executeScript 使用方法
1.使用executeScript 返回一个WebElement .
下例中我们将一个浏览器中的JavaScript 对象返回到客户端(C#,JAVA,Python等)。
IWebElement element = (IWebElement) ((IJavaScriptExecutor)driver).ExecuteScript("return $('.cheese')[0]");
2.使用executeScript 和参数组合返回一个WebElement列表。
下例中我们将客户端中的一个 IList<IWebElement> 对象传递到 浏览器中作为一个 JaveScript 对象使用。
IList<IWebElement> labels = driver.FindElements(By.TagName("label"));
IList<IWebElement> inputs = (IList<IWebElement>) ((IJavaScriptExecutor)driver).ExecuteScript(
"var labels = arguments[0], inputs = []; for (var i=0; i < labels.length; i++){" +
"inputs.push(document.getElementById(labels[i].getAttribute('for'))); } return inputs;", labels);
函数详细解释:
//
// Summary:
// Executes JavaScript in the context of the currently selected frame or window.
//
// Parameters:
// script:
// The JavaScript code to execute.
//
// args:
// The arguments to the script.
//
// Returns:
// The value returned by the script.
//
// Remarks:
// The OpenQA.Selenium.IJavaScriptExecutor.ExecuteScript(System.String,System.Object[])method
// executes JavaScript in the context of the currently selected frame or window.
// This means that "document" will refer to the current document. If the script
// has a return value, then the following steps will be taken:
// For an HTML element, this method returns a OpenQA.Selenium.IWebElement For a
// number, a System.Int64 is returned For a boolean, a System.Boolean is returned
// For all other cases a System.String is returned. For an array,we check the first
// element, and attempt to return a System.Collections.Generic.List`1 of that type,
// following the rules above. Nested lists are not supported. If the value is null
// or there is no return value, null is returned.
// Arguments must be a number (which will be converted to a System.Int64), a System.Boolean,
// a System.String or a OpenQA.Selenium.IWebElement. 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"
object ExecuteScript(string script, params object[] args);
翻译一下 :
1.作用域:当前frame 或 window ,如果是在frame下执行,应该受到frame 的范围影响。
2.返回值:我们客户端以C#为例
| JavaScript | c# |
| HTML element | OpenQA.Selenium.IWebElement |
| number | System.Int64 |
| boolean | System.Boolean |
| 其它类型 | System.String |
| array | 检查第一个元素,尝试转换为System.Collections.Generic.List`1 |
| null | null |
| 没有返回 | null |
| Nested lists | 不支持 |
3.参数:
| C# | JavaScript |
| OpenQA.Selenium.IWebElement | HTML element |
| System.Int64 | number |
| System.Boolean | boolean |
| System.String | string |
| 其它类型 | thrown exception |
其他
Selenium Webdriver 中的 executeScript 使用方法的更多相关文章
- Selenium_用selenium webdriver实现selenium RC中的类似的方法
最近想总结一下学习selenium webdriver的情况,于是就想用selenium webdriver里面的方法来实现selenium RC中操作的一些方法.目前封装了一个ActionDrive ...
- Selenium WebDriver中一些鼠标和键盘事件的使用
转自:http://www.ithov.com/linux/133271.shtml 在使用 Selenium WebDriver 做自动化测试的时候,会经常模拟鼠标和键盘的一些行为.比如使用鼠标单击 ...
- Selenium WebDriver 中鼠标和键盘事件分析及扩展(转)
本文将总结 Selenium WebDriver 中的一些鼠标和键盘事件的使用,以及组合键的使用,并且将介绍 WebDriver 中没有实现的键盘事件(Keys 枚举中没有列举的按键)的扩展.举例说明 ...
- 转:总结Selenium WebDriver中一些鼠标和键盘事件的使用
在使用 Selenium WebDriver 做自动化测试的时候,会经常模拟鼠标和键盘的一些行为.比如使用鼠标单击.双击.右击.拖拽等动作:或者键盘输入.快捷键使用.组合键使用等模拟键盘的操作.在 W ...
- Selenium WebDriver 中鼠标和键盘事件分析及扩展[转载]
原文:http://www.ibm.com/developerworks/cn/java/j-lo-keyboard/ 概念 在使用 Selenium WebDriver 做自动化测试的时候,会经常模 ...
- 总结Selenium WebDriver中一些鼠标和键盘事件的使用
在使用 Selenium WebDriver 做自动化测试的时候,会经常模拟鼠标和键盘的一些行为.比如使用鼠标单击.双击.右击.拖拽等动作:或者键盘输入.快捷键使用.组合键使用等模拟键盘的操作.在 W ...
- Selenium WebDriver 中鼠标和键盘事件分析及扩展
[From] http://www.51testing.com/html/18/631118-861557.html 在使用 Selenium WebDriver 做自动化测试的时候,会经常模拟鼠标和 ...
- Selenium WebDriver 中鼠标事件(全)
Selenium WebDriver 中鼠标事件(全) 鼠标点击操作 鼠标点击事件有以下几种类型: 清单 1. 鼠标左键点击 Actions action = new Actions(driv ...
- (java)selenium webdriver学习---三种等待时间方法:显式等待,隐式等待,强制等待
selenium webdriver学习---三种等待时间方法:显式等待,隐式等待,强制等待 本例包括窗口最大化,刷新,切换到指定窗口,后退,前进,获取当前窗口url等操作: import java. ...
随机推荐
- spring面试问题与答案集锦
我收集了一些spring面试的问题,这些问题可能会在下一次技术面试中遇到.对于其他spring模块,我将单独分享面试问题和答案. 如果你能将在以前面试中碰到的,且你认为这些应该是一个有spring经验 ...
- Docker在Linux上运行NetCore系列(五)更新应用程序
转发请注明此文章作者与路径,请尊重原著,违者必究. 本篇文章与其它系列文章不同,为了方便测试,新建了一个ASP.Net Core视图应用. 备注:下面说的应用,只是在容器中运行的应用程序. 查看现在运 ...
- 《C#并发编程经典实例》学习笔记-关于并发编程的几个误解
误解一:并发就是多线程 实际上多线程只是并发编程的一种形式,在C#中还有很多更实用.更方便的并发编程技术,包括异步编程.并行编程.TPL 数据流.响应式编程等. 误解二:只有大型服务器程序才需要考虑并 ...
- Android Studio 日志工具
在项目中提供5个方法打印日志 Log.v() 最常见的日志信息 Log.d() 调试信息 Log.i() 用于打印用户操作行为 Log.w()警告潜在风险 Log.e()报错信息 TAG 填入类名就好 ...
- frp 初探
条件: (1) 服务器端要有公网 IP (2) 客户端能上网,能够访问服务器的公网 IP 下载 https://github.com/fatedier/frp/releases 根据服务器和客户端的操 ...
- C#中关闭子窗口而不释放子窗口对象的方法
1 在主窗口中实例化子窗口 在主窗口中实例化子窗口,而不是在按钮中实例化子窗口对象. Form2 f2 = new Form2(); 2 通过按钮来显示主窗口 在按钮中需要实现的是窗口的显示 priv ...
- Git合并指定文件到另一个分支
经常被问到如何从一个分支合并特定的文件到另一个分支.其实,只合并你需要的那些commits,不需要的commits就不合并进去了. 合并某个分支上的单个commit 首先,用git log或sourc ...
- 【Java】Properties文件的解析
public abstract class ReadProperties { public ReadProperties() {} /** * 回调函数,由调用者处理 * @param key * @ ...
- Java8 方法引用
概述 方法引用是用来直接访问类或实例阴茎存在的方法或者构造方法.它需要由兼容的函数式接口(lambda表达式中用到的接口)构成的目标类型上下文. 有时候, 当我们想要实现一个函数式接口的方法, 但是已 ...
- C# % 和 /
/相当于整数除法中的除号,%相当于余号5 除以 2 = 2 余 1,因此 5/2=2,5%2=1.