SeleniumWebdriver中的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"));
javascript下载教程
IList<IWebElement> inputs = (IList<IWebElement>) ((IJavaScriptExecutor)driver).ExecuteScript(
"var labels = arguments[0], inputs = []; for (var i=0; i < labels.length; i++){" +
"inputs.ElementById(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.Int64number
System.Boolean boolean
System.String string
其它类型 thrown exception
其他

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。