我想從網站下載圖像使用AutoIT(控制操作系統彈出窗口)和Selenium Webdriver(打開網站從我在哪裏試圖下載圖片)。
我收到OS彈出窗口,並通過使用AutoIt的,我能夠發送新的位置保存文件即,無法保存圖像在所需的位置/文件夾使用AutoIT與硒Webdriver
C:\Users\Casper\Desktop\Resume\Pic.jpg
但是,一旦腳本點擊保存按鈕的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);
Java代碼Webdriver-
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
正如你可以看到它是succsesfully發送新地址操作系統窗口彈出(紅色圓圈內),但點擊保存按鈕後,圖像正在不同位置下載C:\Users\Casper\Downloads (my default download folder)
用不同的名稱-rKZOx
的名字是什麼如果你手動進行,會發生什麼?一樣?如果它根本不起作用,你可以用FileMove移動文件:-) – Xenobiologist
@ Xenobiologist否如果我嘗試手動將它保存到所需的位置,即C:\ Users \ Casper \ Desktop \ Resume名爲Pic –
嘗試設置download.dir首選項將其下載到所需的目錄,然後執行您的任務。 –