无法使用Autoit将文件保存在指定位置

时间:2015-11-18 10:33:20

标签: selenium selenium-webdriver automation autoit

按照以下步骤将文件保存在所需位置:

步骤1:“另存为”窗口打开(使用默认下载位置,文件名为DOC)

步骤2:输入文件名为" D:\ temp \ sample.pdf" (将在编辑栏中输入)

步骤3:点击保存按钮(点击按钮,文件在默认位置下载而不是" D:\ temp"位置)

我使用下面的.au3脚本创建了一个.exe

WinWait("[CLASS:#32770]","",10)
Sleep(2000)
ControlSetText("Save As", "", "Edit1", $CmdLine[1])
Sleep(5000)
ControlClick("Save As", "", "Button1");
Sleep(5000)

单击“保存”后,它将保存在默认位置而不是指定位置。

以下代码,执行脚本。

IO.popen('autoit_script.exe D:\temp') #Ruby Code

有没有办法解决它?

1 个答案:

答案 0 :(得分:1)

这取决于您尝试自动化的软件,但通常会发生这种情况,因为软件无法识别文件保存路径框中的更改。问题在于ControlSetText的工作原理。尝试使用ControlSend进行一些错误检查,以确保您尝试设置的文件路径正确。有时您必须使用一些不同的变体来查看您正在自动化的软件的功能。您可以尝试以下两个示例:

示例一:

WinWait("[CLASS:#32770]", "", 10)

If Not @error Then ;make sure the window was found

     $hWin = WinGetHandle("[CLASS:#32770]") ;get window handle

     ControlSetText($hWin, "", "Edit1", $CmdLine[1]) ;set text
     ControlFocus($hWin, "", "Edit1") ;just to make sure we have focus
     ControlSend($hWin, "", "Edit1", "{ENTER}")) ;should work like click button 1 but you will have to check

    ;better then a sleep
    $hTimer = TimerInit() ; Begin the timer and store the handle in a variable.
    Do
    Until WinExists($hWin) = 0 Or TimerDiff($hTimer) >= 10000
EndIf

示例二:

WinWait("[CLASS:#32770]", "", 10)

If Not @error Then ;make sure the window was found

    $hWin = WinGetHandle("[CLASS:#32770]") ;get window handle

    While 1
        ControlSetText($hWin, "", "Edit1", "") ;just makes sure there is no text in the control text
        ControlFocus($hWin, "", "Edit1") ;just to make sure we have focus
        ControlSend($hWin, "", "Edit1", $CmdLine[1])) ;set text using ControlSend

        If ControlGetText($hWin, "", "Edit1") = $CmdLine[1] Then ExitLoop ;makes sure that the control got ALL of the text before exiting loop
    WEnd

    ControlClick($hWin, "", "Button1");

    ;better then a sleep
    $hTimer = TimerInit() ; Begin the timer and store the handle in a variable.
    Do
    Until WinExists($hWin) = 0 Or TimerDiff($hTimer) >= 10000
EndIf