[英]How to execute executeScript method through Java in Microsoft Edge through WebDriver
I am trying to run below code in Microsoft Edge through WebDriver我正在尝试通过 WebDriver 在 Microsoft Edge 中运行以下代码
ExpectedCondition<Boolean> jsLoad = driver -> ((JavascriptExecutor) driver).executeScript("return
document.readyState").toString().equals("complete");
JavascriptExecutor js = (JavascriptExecutor) getDriver();
boolean jsReady = (Boolean) js.executeScript("return document.readyState").toString().equals("complete");
We are getting below exception:我们遇到以下异常:
Exception class:org.openqa.selenium.JavascriptException
The reason is:原因是:
org.openqa.selenium.JavascriptException: javascript error: Function is not a constructor
We set EdgeOptions like below我们像下面这样设置 EdgeOptions
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setBrowserName("MicrosoftEdge");
EdgeOptions edgeOptions = new EdgeOptions();
edgeOptions.setCapability("ms:inPrivate", true);
edgeOptions.setCapability("prefs", edgePrefs);
edgeOptions.setCapability("useAutomationExtension", false);
edgeOptions.merge(desiredCapabilities);
edgeOptions.setPageLoadStrategy("eager");
edgeOptions.setCapability("ms:inPrivate", true);
edgeOptions.setCapability("useAutomationExtension", false);
edgeOptions.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);
edgeOptions.setCapability(CapabilityType.HAS_NATIVE_EVENTS, true);
driver = new EdgeDriver(edgeOptions);
Any suggestions有什么建议么
Executes JavaScript in the context of the currently selected frame or window. The script fragment provided will be executed as the body of an anonymous function. Within the script, use document to refer to the current document.
在当前选定的框架或 window 的上下文中执行 JavaScript。提供的脚本片段将作为匿名 function 的主体执行。在脚本中,使用 document 来引用当前文档。 Note that local variables will not be available once the script has finished executing, though global variables will persist.
请注意,一旦脚本执行完毕,局部变量将不可用,但全局变量将持续存在。 If the script has a return value (ie if the script contains a return statement), then the following steps will be taken:
如果脚本有返回值(即如果脚本包含返回语句),那么将执行以下步骤:
- For an HTML element, this method returns a WebElement
对于 HTML 元素,此方法返回一个 WebElement
- For a decimal, a Double is returned
对于小数,返回 Double
- For a non-decimal number, a Long is returned
对于非十进制数,返回 Long
- For a boolean, a Boolean is returned
对于 boolean,返回 Boolean
- For all other cases, a String is returned.
对于所有其他情况,返回一个字符串。
- For an array, return a List with each object following the rules above.
对于数组,按照上述规则返回一个列表,其中每个 object。 We support nested lists.
我们支持嵌套列表。
- For a map, return a Map<String, Object> with values following the rules above.
对于 map,返回一个 Map<String, Object>,其值遵循上述规则。
- Unless the value is null or there is no return value, in which null is returned
除非值为null或者没有返回值,其中返回null
Arguments must be a number, a boolean, a String, WebElement, or a List of any combination of the above.
Arguments 必须是数字、boolean、字符串、WebElement 或上述任意组合的列表。 An exception will be thrown if the arguments do not meet these criteria.
如果 arguments 不满足这些条件,将抛出异常。 The arguments will be made available to the JavaScript via the "arguments" magic variable, as if the function were called via "Function.apply"
arguments 将通过“arguments”魔法变量提供给 JavaScript,就好像 function 是通过“Function.apply”调用的一样
If your usecase is to wait for document.readyState
to be equal to complete
you can induce WebDriverWait as follows:如果您的用例是等待
document.readyState
等于complete
,您可以按如下方式引入WebDriverWait :
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(d => ((IJavaScriptExecutor)d).ExecuteScript("return document.readyState").Equals("complete"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.