1

我想從網站下載圖像使用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 

ScreenShot during exxecution of Program

正如你可以看到它是succsesfully發送新地址操作系統窗口彈出(紅色圓圈內),但點擊保存按鈕後,圖像正在不同位置下載C:\Users\Casper\Downloads (my default download folder)用不同的名稱-rKZOx

+0

的名字是什麼如果你手動進行,會發生什麼?一樣?如果它根本不起作用,你可以用FileMove移動文件:-) – Xenobiologist

+0

@ Xenobiologist否如果我嘗試手動將它保存到所需的位置,即C:\ Users \ Casper \ Desktop \ Resume名爲Pic –

+0

嘗試設置download.dir首選項將其下載到所需的目錄,然後執行您的任務。 –

回答

2

現在,我得到了答案。由於我沒有等待窗口正常打開,我無法在我想要的位置下載文件。我只是把線程等待2秒,現在它的工作正常,並保存圖像在所需的位置。改變的代碼是 -

代碼的其餘保持相同下面的代碼被改變 -

Thread.wait(2000); 
    String command ="C:\\Users\\Casper\\Desktop\\Resume\\Pic.exe"; 
    Runtime.getRuntime().exec(command); 

,現在我能在E盤保存圖片與文件PIC

1

也許嘗試這樣的事:

Global $goExplorer = _myExplorerSelectUpload("C:\Users\Casper\Desktop\Resume", "Pic.exe", "Save As") 
If @error Then Exit 101 

ControlClick(HWnd($goExplorer.hwnd),"","[CLASS:Button; INSTANCE:1]") 

Func _myExplorerSelectUpload($szDirectory, $szFileName, $vWndOrTitle, $sText = "") 

    Local $oExplorer = _explorerWinFindObj($vWndOrTitle, $sText) 
    If @error Then Return SetError(@error, @extended, 0) 

    $oExplorer.Navigate($szDirectory) 
    If @error Then Return SetError(3, 0, 0) 
    ; might try a sleep here if it's rendering too fast 

    $oExplorer.document.SelectItem(_ 
     $oExplorer.document.Folder.ParseName($szFileName), 1 + 4 + 8 + 16) 
    If @error Then Return SetError(5, 0, 0) 

    ; return the object you're working with 
    Return $oExplorer 
EndFunc 

Func _explorerWinFindObj($vWndOrTitle, $sText = "") 

    Local $oShell = ObjCreate("Shell.Application") 
    If Not IsObj($oShell) Then 
     Return SetError(1, 0, 0) 
    EndIf 

    Local $oWins = $oShell.windows 
    Local $hWnd, $vDummy 

    For $oWin In $oWins 

     ; browser confirmation - start 
     $vDummy = $oWin.type 
     If Not @error Then ContinueLoop ; if not/browser 

     $vDummy = $oWin.document.title 
     If Not @error Then ContinueLoop 
     ; browser confirmation - end 

     ; bypassed IE windows, now to find window 
     $hWnd = HWnd($oWin.hwnd) 
     If IsHWnd($vWndOrTitle) Then 
      ; hwnd was passed, does it equal hwnd of object 
      If $hWnd = $vWndOrTitle Then Return $oWin 
     Else 
      ; match titles (exact match) 
      If WinGetTitle($hWnd) = $vWndOrTitle Then 
       ; match text, only in string text match 
       If $sText And Not _ 
        StringInStr(WinGetText($hWnd), $sText) Then 
        ContinueLoop 
       EndIf 
       Return $oWin 
       ; hwnd to hwnd 
      ElseIf WinGetHandle($vWndOrTitle, $sText) = $hWnd Then 
       Return $oWin 
      EndIf 
     EndIf 
    Next 

    Return SetError(2, 0, 0) 
EndFunc 
相關問題