簡體   English   中英

使用 autoit 和 selenium 在另存為對話框中重命名文件名

[英]Renaming filename in save as dialog box using autoit with selenium

我正在使用帶有硒的 AutoIt 工具。 我正在做的是當我在我的應用程序中得到一個“另存為”對話框時,我得到了一些默認值,文件存儲在我的系統中。 我正在嘗試將其重命名為“新”,如下面的代碼中所述。 但是我在這里遇到的問題是,文件名在對話框中成功更改為“新建”,但是當我單擊“保存”時,它會以默認文件名存儲。

$windowHandle = WinGetHandle("Enter name of file to save to…")
WinActivate($windowHandle)
ControlSetText("Enter name of file to save to…", "", "Edit1", "New")
ControlClick("Enter name of file to save to…", "", "Button1")

它適用於:

$windowHandle = WinGetHandle("Enter name of file to save to…")
WinActivate($windowHandle)

ControlSetText("Enter name of file to save to…", "", "Edit1", "2131221")
ControlClick("Enter name of file to save to…", "", "Edit1")
ControlSetText("Enter name of file to save to…", "", "Edit1", "5666")
ControlClick("Enter name of file to save to…", "", "Edit1")
ControlSetText("Enter name of file to save to…", "", "Edit1", Send( "  {BACKSPACE}" ))
ControlSetText("Enter name of file to save to…", "", "Edit1", "New")
ControlClick("Enter name of file to save to…", "", "Button1")

但是我在這里遇到的問題是,文件名在對話框中成功更改為“新建”,但是當我單擊“保存”時,它會以默認文件名存儲。

我花了一段時間,但終於為我的項目找到了它,希望有人能從中受益。 事實證明,文件夾路徑的地址欄是(工具欄按鈕和編輯框)的組合。 我不知道實際的結構,但我想象它的方式是; 編輯框嵌套在 ToolbarWindow32 內。 當地址欄(ToolbarWindow32)被點擊手動輸入你自己的路徑時,編輯框被激活。 想象一下 ToolbarWindow32 是父級,編輯框是子級。 任何有知識的人請對此有所了解。

這是一個工作示例,其中包含完成同一件事的不同方式。

    #include <GuiToolbar.au3>
    #Include <File.au3>


    TestChangeSaveAsAddress()

    Func TestChangeSaveAsAddress()

        Run('Notepad.exe')
        WinWaitActive('Untitled - Notepad')
        Send('new line.')
        Send('^s')


        Local $AddressPath = 'K:\_DOC\_TEXT'
        Local $aFileName = 'New File.txt'
        ClipPut($AddressPath)                    ;for  Option 1

        Local $SaveAsTitle = '[CLASS:#32770; TITLE:Save As]'


    # I.a

        ;get 'Save As' window handle
        Local $hWnd = WinWaitActive($SaveAsTitle, '&Save', 10)

    # I.b

        ;get Address Bar handle for $AddressPath
        Local $hTollbars = ControlGetHandle($hWnd, 'Address', '[CLASSNN:ToolbarWindow324]')


    # II - IMPORTANT: Address Bar must be infocus in order to activate Edit2 to set $AddressPath in step (IV)
           ;Option 2 or 3 is highly recommended


        # ;Option 1 - Select ToolbarWindow32 - Address Bar (work around -not recomended)
        #------------------------------------------------------------------------------
    ;~         ControlFocus($hTollbars, 'Address', '')
    ;~         ControlSend($hTollbars, 'Address', '','{SPACE}')
    ;~         Send('^v{ENTER}')

        # ;Option 2 - Select ToolbarWindow32 - Address Bar (same as Option 3)
        #-------------------------------------------------------------------
             ControlCommand($hTollbars, "Address", "", "SendCommandID", 1280)

        # ;Option 3 - Select ToolbarWindow32 - Address Bar (same as Option 2)
        #------------------------------------------------------------------------------------------
    ;~         ControlCommand($hWnd, "Address", "ToolbarWindow324", "SendCommandID", 1280)

        # ;Option 4 - Select ToolbarWindow32 - Address Bar (mouse pointer also relocated)
        #------------------------------------------------------------------------------------------
    ;~        _GUICtrlToolbar_ClickIndex($hTollbars, -1,'Left',True,1,0)

        # ;Option 5 - Select ToolbarWindow32 - Address Bar (this simulate as Run, NOT RunWait if your project required it - NOT Recommended)
        #------------------------------------------------------------------------------------------
    ;~      Run(@AutoItExe & ' /AutoIt3ExecuteLine "ControlCommand ( hwnd(' & $hWnd & '),'''', hwnd(' & $hTollbars & '), ''SendCommandID'', 1280 )"')


    # III
            ;check if path $AddressPath exists
            If Not FileExists($AddressPath) Then DirCreate($AddressPath)

    # IV
            ;set new path
            ControlSetText($hWnd, "Address", 'Edit2', $AddressPath)
    ;~         ControlFocus($hTollbars,'Address','')


    # V
            ;commit new Path
            ControlSend($hWnd, "Address", 'Edit2','{ENTER}')

    # VI
            ;set new file name
            ControlSetText($hWnd, "Namespace", "Edit1", $aFileName)
            ControlFocus($hWnd,'Namespace','Edit1')

    # VII
            ;allow manual keypress {SPACE} or {ENTER} to save/cancel
    ;~         ControlFocus($hWnd,'&Save','Button1')
    ;~         ControlFocus($hWnd,'Cancel','Button2')

    # VIII
            ;auto click save/cancel
    ;~         ControlClick($hWnd,"&Save", 'Button1','Left')
    ;~         ControlClick($hWnd,"Cancel", 'Button2','Left')


    # IX
        #--------------------------------------------------
        # ---OR--- skip all above steps and use this work around
        #--------------------------------------------------
    ;~     ControlSetText($hWnd, "Namespace", "Edit1", $AddressPath&'\'&$aFileName)

    EndFunc

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM