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. ...
随机推荐
- java for循环里面执行sql语句操作,有效结果只有一次,只执行了一次sql mybatis 循环执行update生效一次 实际只执行一次
java后台controller中,for循环执行数据库操作,但是发现实际仅仅执行了一次,或者说提交成功了一次,并没有实际的个数循环 有可能是同一个对象导致的 可以仔细看一下下面两段代码有什么区别 p ...
- Java编译与反编译
Javac是一种编译器,它的作用是将符合Java语言规范的源代码转化成符合Java虚拟机规范的Java字节码. 一.编译过程 词法分析:找出源代码中的所有关键字,生成Token流 语法分析:检查Tok ...
- Docker在Linux上运行NetCore系列(一)配置运行DotNetCore控制台
转发请注明此文章作者与路径,请尊重原著,违者必究. 系列文章:https://www.cnblogs.com/alunchen/p/10121379.html 本篇文章操作系统信息 Linux:ubu ...
- Visual Studio 2017 - Windows应用程序打包成exe文件(1)- 工具简单总结
最近有对一个Windows应用程序少许维护和修改.修改之后要发布新的exe安装文件,打包exe文件时,遇到了很头疼的问题,还好最后解决了,记录一下. Visual Studio版本:Visual St ...
- 【转载】C#工具类:FTP操作辅助类FTPHelper
FTP是一个8位的客户端-服务器协议,能操作任何类型的文件而不需要进一步处理,就像MIME或Unicode一样.可以通过C#中的FtpWebRequest类.NetworkCredential类.We ...
- C# winform自动更新 (附 demo下载)
随着需求的变化,如果Server每次更新出新的内容,Client都要重新安装的话. 太过于复杂化. 所以自动更新是很有必要的. 一..NET自带的更新方式 以服务器端为主 (自动更新,微软爸 ...
- linux下定时执行任务的方法
linux下定时执行任务的方法 在LINUX中你应该先输入crontab -e,然后就会有个vi编辑界面,再输入0 3 * * 1 /clearigame2内容到里面 :wq 保存退出. 在LINUX ...
- 33.QT-UTF8,GBK互转
首先需要用到QString的静态成员函数来获取字符数组: QByteArray QString::toLocal8Bit () ; //获取字节数组对象 char * QByteArray::data ...
- 软件测试工程师这样面试,拿到offer的几率是80%
面试难还是不难?取决于面试者的底蕴(气场+技能).心态和认知及沟通技巧.面试其实可以理解为一场聊天和谈判,在这过程中有心理.思想上的碰撞和博弈.其实你只需要搞清楚一个逻辑:“面试官为什么会这样问?他希 ...
- 【Zookeeper】windows环境下zookeeper安装
下载 Apache官方最新版本为:3.4.12 下载地址:https://mirrors.cnnic.cn/apache/zookeeper/zookeeper-3.4.12/ 安装 解压到指定目录 ...