使用带有Selenium Webdriver的AutoIT无法将图像保存在所需的位置/文件夹中

时间:2015-01-09 08:25:10

标签: java selenium-webdriver download autoit selenium-chromedriver

我正在尝试使用AutoIT(控制操作系统弹出窗口)和Selenium Webdriver从网站下载图像(打开我尝试下载图片的网站)。
我正在获取操作系统弹出窗口并使用AutoIT我能够发送新位置以保存文件

C:\Users\Casper\Desktop\Resume\Pic.jpg  

但是,一旦脚本点击了保存按钮,图片就会被下载,但名称不同,位于不同/默认位置。

我正在使用的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);

Webdriver-

的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 

ScreenShot during exxecution of Program

正如您所看到的那样,将新地址发送到操作系统窗口弹出窗口(红色圆圈内),但在点击“保存”按钮后,图像将被下载到不同位置 C:\Users\Casper\Downloads (my default download folder) 使用不同的名称-rKZOx

2 个答案:

答案 0 :(得分:2)

现在我得到了答案。由于我没有等待窗口正常打开,我无法在我想要的位置下载文件。我只是让一个线程等待2秒,现在它工作正常,并将图像保存在所需的位置。更改的代码是 -

其余代码保持不变,以下代码已更改 -

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

现在我可以在e驱动器上保存图像,文件名为pic

答案 1 :(得分: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