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

Selenium 无法使用Autoit将文件保存在指定位置,selenium,selenium-webdriver,automation,autoit,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]) Sle

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

步骤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

是否有办法解决此问题?

这取决于您尝试自动化的软件,但通常会发生这种情况,因为软件无法识别“文件保存路径”框中有更改。问题在于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

它在单击键盘输入时工作。发送({ENTER}),而不是单击对话框中的“保存”按钮。如果发送功能适合您,则最好将ControlSend与{ENTER}一起使用。从长远来看,它将更加可靠。