简体   繁体   中英

Renaming filename in save as dialog box using autoit with selenium

I am using AutoIt tool with selenium. What I am doing is when I get a 'Save as' dialog box in my application, I get some default value with which the file is stored in my system. I am trying to rename that to 'New' as mentioned in my code below. But the issue that I am getting here is, the file name gets changed to 'New' successfully in the dialog box but when I click on 'Save', it gets stored with the default filename.

$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")

It worked with this:

$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")

But the issue that I am getting here is, the file name gets changed to 'New' successfully in the dialog box but when I click on 'Save', it gets stored with the default filename.

I took me a while but finally figured it out for my project, hope someone could benefit from this. It turns out the Address Bar for folder path is a combo of (Toolbar buttons and an Edit box). I don't know the actual structures but the way I visualizing it is; Edit box is nested inside ToolbarWindow32. When address bar (ToolbarWindow32) is clicked to Manually input your own Path then Edit box is activated. Imagine ToolbarWindow32 is the parent and Edit box is the child. Anyone with the knowledge please shed some light into this.

Here is a working example with different ways of accomplishing the same thing.

    #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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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