有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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 

ScreenShot during exxecution of Program

如您所见,它成功地将新地址发送到操作系统窗口弹出窗口(红色圆圈内),但在单击“保存”按钮后,图像将在不同的位置下载,并使用不同的名称-rKZOx


共 (2) 个答案

  1. # 1 楼答案

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

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

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

    现在我可以在e-drive上保存图片,文件名为pic

  2. # 2 楼答案

    也许可以试试这样:

    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