使用 selenium 的 autoit 在另存为对话框中重命名文件名
Renaming filename in save as dialog box using autoit with selenium
我正在使用 AutoIt 工具和 selenium。我正在做的是当我在我的应用程序中得到一个 'Save as' 对话框时,我得到了一些默认值,文件存储在我的系统中。我正在尝试将其重命名为 'New',如下面的代码中所述。但我遇到的问题是,文件名在对话框中成功更改为 'New',但是当我单击 'Save' 时,它会以默认文件名存储。
$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")
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.
我花了一段时间但终于为我的项目弄明白了,希望有人能从中受益。事实证明,文件夹路径的地址栏是(工具栏按钮和编辑框)的组合。我不知道实际的结构,但我想象它的方式是;编辑框嵌套在 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
我正在使用 AutoIt 工具和 selenium。我正在做的是当我在我的应用程序中得到一个 'Save as' 对话框时,我得到了一些默认值,文件存储在我的系统中。我正在尝试将其重命名为 'New',如下面的代码中所述。但我遇到的问题是,文件名在对话框中成功更改为 'New',但是当我单击 'Save' 时,它会以默认文件名存储。
$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")
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.
我花了一段时间但终于为我的项目弄明白了,希望有人能从中受益。事实证明,文件夹路径的地址栏是(工具栏按钮和编辑框)的组合。我不知道实际的结构,但我想象它的方式是;编辑框嵌套在 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