在使用selenium实现web应用程序的自动化时,我遇到了需要上传文件并继续工作的情况。
为此,我们使用了Java & Tcl脚本语言。
以下是我的TCL代码:
set methodname "uploadFile"
set action "Open"
set file "C:\\\\BATFiles\\\\InsertUsersAccessGroup.txt"
[$_webdriverObj executeScript $methodname $action $file] --> This calls the java method 'executeScript'这里'executeScript‘是我的Java方法,编码如下:
public void executeScript(String methodName, String action,String file) {
log.info("Before try block");
try {
log.info("Inside try block");
Runtime r = Runtime.getRuntime();
log.info("Created a runtime object");
Process p = r.exec(new String[]{"C:\\AutoIt\\ModenaAutoIt.exe", methodName, action, file });
log.info("Afte the exec");
p.waitFor();
} catch(Exception IOException) {
log.info("inside exception");
log.info(IOException);
}
}尽管文件"ModenaAutoIt.exe“存在于'AutoIt‘文件夹下的'C’目录中,但我的脚本在Java中失败了
java.io.IOException:无法运行程序"C:\AutoIt\ModenaAutoIt.exe":java.io.IOException: error=2,没有这样的文件或目录“
有人能帮帮我吗?
发布于 2014-02-18 13:49:14
这段代码在这里工作得很好,也许您可以通过我们的示例调用进行检查。它还包括被调用的可执行文件的输出:
package test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ProcBuilderTest {
public static void main(String[] args) throws Exception {
final ProcessBuilder pb = new ProcessBuilder("C:/WINDOWS/system32/notepad.exe", "d:/tmp/tmp.txt");
pb.redirectErrorStream(true);
final Process p = pb.start();
BufferedReader res = new BufferedReader(new InputStreamReader(p.getInputStream()));
String commandOutput = "";
String tmp;
while ((tmp = res.readLine()) != null) {
commandOutput += tmp;
}
System.out.println("output:" + commandOutput);
if (p.waitFor() != 0) {
System.out.println("exit value is: " + p.exitValue());
return;
}
p.destroy();
}
}发布于 2015-01-09 04:10:51
我今天也有同样的例外。
Exception-
java.io.IOException: Cannot run program ""C:/Users/Casper/Desktop/Down.exe"null": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source) 原因是我没有将.exe文件的确切位置传递给Runtime.getRuntime().exec(命令)方法。
而不是在下面发送地址->>
String command ="\"C:/Users/Casper/Desktop/Resource/Down.exe\""; 我是派->
String command ="\"C:/Users/Casper/Desktop/Down.exe\"";正因为如此,才有了例外。
https://stackoverflow.com/questions/21852503
复制相似问题