简体   繁体   English

使用 autoit 和 selenium 在另存为对话框中重命名文件名

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

I am using AutoIt tool with selenium.我正在使用带有硒的 AutoIt 工具。 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.编辑框嵌套在 ToolbarWindow32 内。 When address bar (ToolbarWindow32) is clicked to Manually input your own Path then Edit box is activated.当地址栏(ToolbarWindow32)被点击手动输入你自己的路径时,编辑框被激活。 Imagine ToolbarWindow32 is the parent and Edit box is the child.想象一下 ToolbarWindow32 是父级,编辑框是子级。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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