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. Docker容器互访三种方式

    我们都知道docker容器之间是互相隔离的,不能互相访问,但如果有些依赖关系的服务要怎么办呢.下面介绍三种方法解决容器互访问题. 方式一.虚拟ip访问 安装docker时,docker会默认创建一个内 ...

  2. 【Vue.js】基于vue的实时搜索,在结果中高亮显示关键词

    一.搜素效果如下: 二.核心 1)利用oninput属性来触发搜素功能 2)利用RegExp来对字符串来全局匹配关键字,利用replace方法来对匹配的关键字进行嵌入高亮的<span class ...

  3. shell脚本示例:计算毫秒级、微秒级时间差

    bash&shell系列文章:http://www.cnblogs.com/f-ck-need-u/p/7048359.html 有时候需要计算命令的执行时间长度,可以使用time命令,虽然t ...

  4. Go Web:处理请求

    处理请求 Request和Response http Requset和Response的内容包括以下几项: Request or response line Zero or more headers ...

  5. 用初中代数结合python画出正方形

    在屏幕上打印类似下面的图形: 常规画正方形的算法: 这几乎是初学所有计算机语言时都会遇到的问题.算法都大致类似,就是找出打印规律然后用计算机语句表达出来.最常规的算法是:输入数字n就打印n行,首行和尾 ...

  6. Web部分_2

    详细描述MVC 基于Java的Web应用系统采用MVC架构模式,即model(模型).view(视图).controller(控制)分离设计:这是目前Web应用服务系统的主流设计方向. Model:即 ...

  7. 《Office 365开发入门指南教程》正式上线,限时优惠和邀请分享推广

    我很高兴地通知大家,<Office 365 开发入门指南教程>已经正式在网易云课堂上线,你可以通过直接访问  https://aka.ms/office365devlesson 这个短地址 ...

  8. js发送邮件确定email地址

    <a href="mailto:wjl@tom.com?subject=aaa&body=11111">test</a>

  9. Java中的匿名内部类及内部类的二三事

    匿名内部类适合创建那些只需要使用一次的类,它的语法有些奇怪,创建匿名内部类会立即创建一个该类的实例,这个类定义立即消失,且不能重复使用. 定义匿名类的格式如下: new 实现接口() |父类构造器(实 ...

  10. 判断文本是否溢出/hover显示全部

    前言 在工作中我们经常会遇到,文字过多,需要用省略号,并且鼠标hover的时候 还需要 显示全部的文字的需求. 正文 文字过多需要用省略号的实现:上代码啦 .ellipsis { width: 100 ...