简体   繁体   English

如何通过WebDriver在Microsoft Edge中通过Java执行executeScript方法

[英]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有什么建议么

executeScript() 执行脚本()

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”调用的一样


This usecase这个用例

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.

相关问题 当通过 Selenium WebDriver 使用来自 JavascriptExecutor 接口的 executeScript 方法时,arguments[0] 和 arguments[1] 是什么意思? - What does arguments[0] and arguments[1] mean when using executeScript method from JavascriptExecutor interface through Selenium WebDriver? 通过Selenium和Python通过WebDriver实例调用execute_script()方法时,arguments [0]是什么? - What is arguments[0] while invoking execute_script() method through WebDriver instance through Selenium and Python? 如何使用Selenium Java Web驱动程序的executeScript方法执行用户定义的JavaScript函数 - How to execute a user defined JavaScript function using executeScript method of Selenium Java web driver 通过WebDriver在C#中执行Javascript文件 - Execute Javascript file in C# through WebDriver 滚动执行脚本的量角器不工作 - Protractor scrolling through executeScript not working executeScript WebDriver - executeScript WebDriver 如何从 Java 中的 JavascriptExecutor.executeScript() 方法返回值 - How to return value from JavascriptExecutor.executeScript() method in java Selenium&webdriver.io如何使用executeScript? - Selenium & webdriver.io how to use executeScript? 如何在通过对象传递的指令中执行方法 - How to execute a method in directive which is passed through an object 在谷歌浏览器中通过executeScript注入多个脚本 - Injecting multiple scripts through executeScript in Google Chrome
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM