我写了一个Selenium脚本来下载Excel工作表,打开,编辑并上传回来。我使用AutoIt进行文件上传。
我为每次运行都要上传不同的文件,因此我无法对文件路径进行硬编码,因此我想将其作为参数传递给AutoIt脚本。 AutoIt脚本:
ControlFocus("File Upload", "", "Edit1")
Sleep(1000)
ControlSetText("File Upload", "", "Edit1", "C:\Users\nilasing\Downloads\somefilename")
Sleep(1000)
ControlClick("File Upload", "", "Button1")
"C:\Users\nilasing\Downloads\somefilename"
是硬编码路径。
答案 0 :(得分:2)
您可以将命令行参数传递给脚本,然后使用$CmdLine
数组读取它们。
ControlFocus("File Upload", "", "Edit1")
Sleep(1000)
ControlSetText("File Upload", "", "Edit1", $CmdLine[1])
Sleep(1000)
ControlClick("File Upload", "" , "Button1")
有关详细信息,请参阅https://www.autoitscript.com/autoit3/docs/intro/running.htm#CommandLine。
答案 1 :(得分:2)
我使用了相同的方法: 在我的AutoIT au3文件中 -
ControlFocus("File Upload","","Edit1")
ControlSetText("File Upload","","Edit1",$CmdLine[1])
ControlClick("File Upload","","Button1")
然后使用 -
从Selenium WebDriver调用AutoIT可执行文件String fileName = "C:\\Calendar.xls"; // passed as a command line parameter to AutoIT executable below
String autoITExecutable = ".\\AutoITFileUploadWithParam.exe " + fileName;
.......
try {
Runtime.getRuntime().exec(autoITExecutable);
Thread.sleep(5000);
test.log(LogStatus.INFO, "Ran AutoIT script to upload : " + fileName);
} catch (Exception e) {
test.log(LogStatus.ERROR, "Failed to run AutoIT script : " + e.getMessage());
}
driver.findElement(By.xpath("//div[text()='Send']")).click();
test.log(LogStatus.INFO, "Email sent");
请注意AutoIT可执行文件与String autoITExecutable
中的参数之间的空格