java无法使用AutoIT和Selenium Webdriver在所需位置/文件夹保存图像
我试图使用AutoIT(控制操作系统弹出窗口)和Selenium Webdriver(打开我试图下载pic的网站)从网站下载图像
我获得操作系统弹出窗口,通过使用AutoIT,我能够发送保存文件的新位置,即
C:\Users\Casper\Desktop\Resume\Pic.jpg
但是一旦脚本点击save按钮,pic就会被下载,但名称和默认位置不同
我正在使用的AutoIT脚本写在下面-
WinWait("Save As");
WinActive("Save As");
Sleep(1000);
ControlSetText("Save As","","[CLASS:Edit; INSTANCE:1]","C:\Users\Casper\Desktop\Resume\Pic.jpg");
Sleep(1000);
ControlClick("Save As","","[CLASS:Button; INSTANCE:1]");
Sleep(1000);
Web驱动程序的Java代码-
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class Practice {
public void pic() throws AWTException, IOException, InterruptedException{
WebDriver driver;
System.setProperty("webdriver.chrome.driver","E:\\chromedriver.exe");
driver = new ChromeDriver();
try{
driver.navigate().to("http://i.stack.imgur.com/rKZOx.jpg?s=128&g=1");
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("/html/body/img"))).perform();
action.contextClick().perform();
Robot robo = new Robot();
robo.keyPress(KeyEvent.VK_V);
robo.keyRelease(KeyEvent.VK_V);
// Here i am getting the os window but don't know how to send the desired location
String command ="C:\\Users\\Casper\\Desktop\\Resume\\Pic.exe";
Runtime.getRuntime().exec(command);
}catch(Exception e){
e.printStackTrace();
driver.close();
}//catch
finally{
Thread.sleep(6000);
System.out.println("command");
driver.quit();
System.exit(0);
}
}//method
如您所见,它成功地将新地址发送到操作系统窗口弹出窗口(红色圆圈内),但在单击“保存”按钮后,图像将在不同的位置下载,并使用不同的名称-rKZOx
# 1 楼答案
现在我得到了答案。由于我没有等待窗口正确打开,因此无法在所需位置下载文件。我只是把一个线程等待2秒钟,现在它的工作良好,并保存在所需的位置图像。更改代码为-
其余代码保持不变。以下代码已更改-
现在我可以在e-drive上保存图片,文件名为pic
# 2 楼答案
也许可以试试这样: