简体   繁体   English

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

[英]Not able to save image at desired location/folder using AutoIT with Selenium Webdriver

I am trying to download an image from a website using AutoIT(to control OS Pop up Window) and Selenium Webdriver(to open the website from where i am trying to download the pic). 我正在尝试使用AutoIT(控制操作系统弹出窗口)和Selenium Webdriver从网站下载图像(打开我试图下载图片的网站)。
I am getting the OS Pop Up window and by using AutoIT i am able to send the new location for saving the file ie , 我正在获取操作系统弹出窗口并通过使用AutoIT我能够发送新位置以保存文件,

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

But once the script clicks on the save button the pic get downloaded but with a different name and at a different/default location. 但是,一旦脚本点击了保存按钮,图片就会被下载,但名称不同,位于不同/默认位置。

AutoIT Script which i am using is written below- 我正在使用的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 code for Webdriver- 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

As you can see it is succsesfully sending the new address to the OS Window Pop (inside red circle) but after clicking on Save button the image is getting downloaded at different location C:\\Users\\Casper\\Downloads (my default download folder) with a different name -rKZOx 正如您所看到的那样,将新地址发送到OS Window Pop(红色圆圈内),但点击“保存”按钮后,图像将被下载到不同的位置C:\\Users\\Casper\\Downloads (my default download folder)一个不同的名字-rKZOx

Now i got the answer. 现在我得到了答案。 Since i was not waiting for the window to open properly i was not able to download the file at my desired location. 由于我没有等待窗口正常打开,我无法在我想要的位置下载文件。 I just put a Thread wait for 2 seconds and now its working fine and saving the image at the desired location. 我只是让一个线程等待2秒,现在它工作正常,并将图像保存在所需的位置。 Changed code is- 更改的代码是 -

Rest of the code remain same the below code is changed - 其余代码保持不变,以下代码更改 -

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

And now i am able to save image at e drive with the name of the file as pic 现在我能够将文件名称保存为电子驱动器上的图像

Maybe try something like this: 也许尝试这样的事情:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM