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 使用方法的更多相关文章

  1. Selenium_用selenium webdriver实现selenium RC中的类似的方法

    最近想总结一下学习selenium webdriver的情况,于是就想用selenium webdriver里面的方法来实现selenium RC中操作的一些方法.目前封装了一个ActionDrive ...

  2. Selenium WebDriver中一些鼠标和键盘事件的使用

    转自:http://www.ithov.com/linux/133271.shtml 在使用 Selenium WebDriver 做自动化测试的时候,会经常模拟鼠标和键盘的一些行为.比如使用鼠标单击 ...

  3. Selenium WebDriver 中鼠标和键盘事件分析及扩展(转)

    本文将总结 Selenium WebDriver 中的一些鼠标和键盘事件的使用,以及组合键的使用,并且将介绍 WebDriver 中没有实现的键盘事件(Keys 枚举中没有列举的按键)的扩展.举例说明 ...

  4. 转:总结Selenium WebDriver中一些鼠标和键盘事件的使用

    在使用 Selenium WebDriver 做自动化测试的时候,会经常模拟鼠标和键盘的一些行为.比如使用鼠标单击.双击.右击.拖拽等动作:或者键盘输入.快捷键使用.组合键使用等模拟键盘的操作.在 W ...

  5. Selenium WebDriver 中鼠标和键盘事件分析及扩展[转载]

    原文:http://www.ibm.com/developerworks/cn/java/j-lo-keyboard/ 概念 在使用 Selenium WebDriver 做自动化测试的时候,会经常模 ...

  6. 总结Selenium WebDriver中一些鼠标和键盘事件的使用

    在使用 Selenium WebDriver 做自动化测试的时候,会经常模拟鼠标和键盘的一些行为.比如使用鼠标单击.双击.右击.拖拽等动作:或者键盘输入.快捷键使用.组合键使用等模拟键盘的操作.在 W ...

  7. Selenium WebDriver 中鼠标和键盘事件分析及扩展

    [From] http://www.51testing.com/html/18/631118-861557.html 在使用 Selenium WebDriver 做自动化测试的时候,会经常模拟鼠标和 ...

  8. Selenium WebDriver 中鼠标事件(全)

    Selenium WebDriver 中鼠标事件(全) 鼠标点击操作  鼠标点击事件有以下几种类型:  清单 1. 鼠标左键点击   Actions action = new Actions(driv ...

  9. (java)selenium webdriver学习---三种等待时间方法:显式等待,隐式等待,强制等待

    selenium webdriver学习---三种等待时间方法:显式等待,隐式等待,强制等待 本例包括窗口最大化,刷新,切换到指定窗口,后退,前进,获取当前窗口url等操作: import java. ...

随机推荐

  1. Maven三种仓库的配置

    转自:https://www.cnblogs.com/jack1995/p/6925879.html Maven三种仓库的配置 1 本地仓库的配置 在第一篇中我们介绍过,Maven的仓库有三类,这里不 ...

  2. C# 通过java生成的RSA公钥加密和解密

    最近工作需要将对方公司生成的RSA加密公钥进行明文加密和解密,发现了几点贴出来做个笔记. RSA单次加密是有长度限制!微软封装的加密方法如果出现长度超出指定范围的话报错是直接报“该项不适于在指定状态下 ...

  3. Go基础系列:defer、panic和recover

    defer关键字 defer关键字可以让函数或语句延迟到函数语句块的最结尾时,即即将退出函数时执行,即便函数中途报错结束.即便已经panic().即便函数已经return了,也都会执行defer所推迟 ...

  4. iOS 快速排序

    一.快速排序概念及其思想 快速排序(QuickSort),又称为交换排序,是分治算法的一种,快速排序采用分治的策略. 1.分治法的基本思想: 将原问题分解为若干个规模更小但结构和原问题相似的子问题.递 ...

  5. [转]usdt omnicore testnet 测试网络

    本文转自:https://www.jianshu.com/p/417c280b8f9f Testnet 模式允许 omni core 运行在比特币测试链上,用于安全测试. 为了在 testnet 上收 ...

  6. 【微服务No.1】Consul服务发现在windows下简单使用

    基本介绍: 安装: 下载地址:https://www.consul.io/downloads.html 运行: consul agent -dev 显示这个界面说明已经开启成功. 页面显示: 然后访问 ...

  7. c# 大批量用户访问数据库报错

    报错信息:There is already an open DataReader associated with this Connection which must be closed first ...

  8. Linux-bg和fg命令(19)

    使用ctrl+z将程序挂在后台: jobs 查看后台的命令: fg(fore go) 将后台的命令,放置前台(fore)继续执行,比如:fg 2     //等价于vi 2.txt bg(back g ...

  9. python基础学习(九)字典

    字典的定义 dictionary(字典) 是 除列表以外 Python 之中 最灵活 的数据类型(有点类似java中的Map) 字典同样可以用来 存储多个数据 通常用于存储 描述一个 物体 的相关信息 ...

  10. JavaScript是如何工作的:事件循环和异步编程的崛起 + 5种使用 async/await 更好地编码方式!

    摘要: 深度理解JS事件循环!!! 原文:JavaScript是如何工作的:事件循环和异步编程的崛起+ 5种使用 async/await 更好地编码方式! 作者:前端小智 Fundebug经授权转载, ...