简体   繁体   English

如何从 Java 中的 JavascriptExecutor.executeScript() 方法返回值

[英]How to return value from JavascriptExecutor.executeScript() method in java

I have written one method in java file and calling one method which is written in java script file with the help org.openqa.selenium.JavascriptExecutor from that java file method.我已经在 java 文件中编写了一种方法,并在org.openqa.selenium.JavascriptExecutor的帮助下从该 java 文件方法调用了一种在 java 脚本文件中编写的方法。 here is the code snippet:这是代码片段:

public void validateFilename() {
    JavascriptExecutor jsExec = (JavascriptExecutor) webDriver;
    jsExec.executeScript("getFileName();");
}


function() {
    window.getFileName = function() {
        var fileName = "sampleFile.txt";
        return fileName;
    }
};

I am able to call method getFileName() from java file but I am able to get the value of file name.我可以从 java 文件调用getFileName()方法,但我可以获取文件名的值。 If I give alert(fileName) it is showing fileName but not able to return that method in java file.如果我给alert(fileName)它显示 fileName 但无法在 java 文件中返回该方法。

Is there any way to return the value from js file to java file with the help of API of selenium JavascriptExecutor ?有没有办法在 selenium JavascriptExecutor的 API 的帮助下将值从 js 文件返回到 java 文件?

JavascriptExecutor.executeScript("<javascript code>") allows you to execute JavaScript code, but when the code you pass to executeScript returns a value, Selenium won't know what the exact return type is at run time as JavaScript can return anything like Number , String , Array , Object etc. JavascriptExecutor.executeScript("<javascript code>")允许您执行 JavaScript 代码,但是当您传递给 executeScript 的代码返回一个值时,Selenium 将不知道运行时的确切返回类型是什么,因为 JavaScript 可以返回类似NumberStringArrayObject等。

To handle all return types, executeScript returns an object of 'Object' class which in turn can handle any return type of JavaScript.为了处理所有返回类型, executeScript返回一个“Object”类的对象,而该对象又可以处理 JavaScript 的任何返回类型。 We can typecast the object returned to any one of following supported objects:我们可以将返回的对象类型转换为以下任何一种支持的对象:

  • For an HTML element in js return, this method returns a WebElement对于js返回的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
  • For all other cases, a String is returned.对于所有其他情况,返回一个String
  • For an array, return a List<Object> with each object following the rules above.对于数组,返回一个List<Object> ,每个对象都遵循上述规则。
  • For a map, return a Map<String, Object> with values following the rules above.对于地图,返回具有遵循上述规则的值的Map<String, Object> Unless the value is null or there is no return value, in which null is returned除非值为null或者没有返回值,其中返回null

You need to cast it to String when trying to get the response.尝试获取响应时,您需要将其转换为 String。 Something like this:像这样的东西:

String fileName = (String) jsExec.executeScript("return getFileName();");

试试下面的代码,

jsExec.executeScript( "return getFileName()");

Try the following:请尝试以下操作:

 String txt = "return document.title";
 JavascriptExecutor js = (JavascriptExecutor) driver;
 String res = (String)js.executeScript(txt);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavascriptExecutor的executeScript()方法的返回类型是什么? - What is the return type of executeScript() method of JavascriptExecutor? 如何从Java中的JavascriptExecutor返回字符串 - how to return a string from JavascriptExecutor in java 当通过 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 JavascriptExecutor 从 Java 中的 JavaScript 返回对象? - Is there a way to return an Object from JavaScript in Java using Selenium JavascriptExecutor? 如何从量角器中使用browser.executeScript执行的js获取返回值? - How to get a return value from a js executing using browser.executeScript in protractor? 我收到错误消息“JavascriptExecutor 类型中的方法 executeScript(String, Object[]) 不适用于参数 (String)” - I am getting error as “The method executeScript(String, Object[]) in the type JavascriptExecutor is not applicable for the arguments (String)” 如何通过WebDriver在Microsoft Edge中通过Java执行executeScript方法 - How to execute executeScript method through Java in Microsoft Edge through WebDriver WebDriver的executeScript返回alert文本而不是JavaScript方法返回的值 - WebDriver's executeScript returns text of alert instead of value returned from JavaScript method Java Selenium JavascriptExecutor - Java Selenium JavascriptExecutor 在 Selenium 和 Java 中使用 JavaScriptExecutor - 它采用该值但不在字段中显示它 - Using JavaScriptExecutor in Selenium with Java - It takes the value but does not display it in the field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM