【问题标题】:How to access the control identifiers in pywinauto如何访问 pywinauto 中的控件标识符
【发布时间】:2011-02-18 09:35:51
【问题描述】:

我正在尝试访问显示的控件标识符,但我无法这样做。当弹出一个打开的窗口时,只有确定和取消按钮我才能访问.. 无法访问其他选项。请帮忙

【问题讨论】:

  • 请更具体一点:您控制的是什么类型的窗口? “其他选项”是否在同一个弹出窗口中?如果不是,很有可能是当前弹窗阻止了对“其他选项”的访问。
  • 嘿,我正在尝试控制 Windows Media Player 的打开窗口。我需要在哪里选择文件或在 PC 中搜索。选择文件类型的选项下拉框。我想要控制整个打开窗口。

标签: python pywinauto


【解决方案1】:

打开对话框并分配给变量后,print_control_identifiers()(也称为 PrintControlIdentifiers)方法应该打印所有可用的控件。

>>> app = Application.Start('Notepad')
>>> app.UntitledNotepad.MenuItem("Edit -> Replace").Select()
>>> app.Replace.print_control_identifiers()
Control Identifiers:
Static - 'Fi&nd what:'   (L1456, T336, R1528, B349)
        'Fi&nd what:' 'Fi&nd what:Static' 'Static' 'Static0' 'Static1'
Edit - ''   (L1531, T332, R1702, B352)
        'Edit' 'Edit0' 'Edit1' 'Fi&nd what:Edit'
Static - 'Re&place with:'   (L1456, T363, R1528, B376)
        'Re&place with:' 'Re&place with:Static' 'Static2'
Edit - ''   (L1531, T360, R1702, B380)
        'Edit2' 'Re&place with:Edit'
Button - 'Match &case'   (L1458, T422, R1547, B442)
        'CheckBox2' 'Match &case' 'Match &caseCheckBox'
Button - '&Find Next'   (L1711, T328, R1786, B351)
        '&Find Next' '&Find NextButton' 'Button' 'Button0' 'Button1'
Button - '&Replace'   (L1711, T355, R1786, B378)
        '&Replace' '&ReplaceButton' 'Button2'
Button - 'Replace &All'   (L1711, T383, R1786, B406)
        'Button3' 'Replace &All' 'Replace &AllButton'
Button - 'Cancel'   (L1711, T410, R1786, B433)
        'Button4' 'Cancel' 'CancelButton'
>>> app.Replace.PrintControlIdentifiers()
Control Identifiers:
Static - 'Fi&nd what:'   (L1456, T336, R1528, B349)
        'Fi&nd what:' 'Fi&nd what:Static' 'Static' 'Static0' 'Static1'
Edit - ''   (L1531, T332, R1702, B352)
        'Edit' 'Edit0' 'Edit1' 'Fi&nd what:Edit'
Static - 'Re&place with:'   (L1456, T363, R1528, B376)
        'Re&place with:' 'Re&place with:Static' 'Static2'
Edit - ''   (L1531, T360, R1702, B380)
        'Edit2' 'Re&place with:Edit'
Button - 'Match &case'   (L1458, T422, R1547, B442)
        'CheckBox2' 'Match &case' 'Match &caseCheckBox'
Button - '&Find Next'   (L1711, T328, R1786, B351)
        '&Find Next' '&Find NextButton' 'Button' 'Button0' 'Button1'
Button - '&Replace'   (L1711, T355, R1786, B378)
        '&Replace' '&ReplaceButton' 'Button2'
Button - 'Replace &All'   (L1711, T383, R1786, B406)
        'Button3' 'Replace &All' 'Replace &AllButton'
Button - 'Cancel'   (L1711, T410, R1786, B433)
        'Button4' 'Cancel' 'CancelButton'

每个控件的第二行输出是不同的标识符(尽管应该删除空格和与号 (&) 之类的东西。

Windows Media Player 很可能不使用“标准”Win32 对话框。但是打开对话框应该很简单..

这是我对此测试的输出(删除了错误和错误以保护有罪的人:))

>>> wmp = Application.Start(r'C:\Program Files (x86)\Windows Media Player\wmplayer.exe')
>>> wmp.WindowsMediaPlayer.TypeKeys("^O")
<pywinauto.controls.win32_controls.DialogWrapper object at 0x027CCDB0>
>>> wmp.Open.PrintControlIdentifiers()
Control Identifiers:
DUIViewWndClassName - ''   (L200, T187, R1152, B590)
        'DUIViewWndClassName' 'Files of &type:DUIViewWndClassName'
DirectUIHWND - ''   (L200, T187, R1152, B590)
        'DirectUIHWND' 'DirectUIHWND0' 'DirectUIHWND1' 'Files of &type:DirectUIHWND' 'Files of &type
:DirectUIHWND0' 'Files of &type:DirectUIHWND1'
CtrlNotifySink - ''   (L201, T217, R426, B590)
        'CtrlNotifySink' 'CtrlNotifySink0' 'CtrlNotifySink1' 'Files of &type:CtrlNotifySink' 'Files
of &type:CtrlNotifySink0' 'Files of &type:CtrlNotifySink1'
NamespaceTreeControl - 'Namespace Tree Control'   (L201, T217, R426, B590)
        'Namespace Tree Control' 'Namespace Tree Control0' 'Namespace Tree Control1' 'Namespace Tree
 ControlNamespaceTreeControl' 'NamespaceTreeControl'
SysTreeView32 - 'Tree View'   (L201, T217, R426, B590)
        'Tree View' 'Tree ViewTreeView' 'TreeView'
CtrlNotifySink - ''   (L429, T217, R1152, B590)
        'CtrlNotifySink3' 'Files of &type:CtrlNotifySink2'
SHELLDLL_DefView - 'ShellView'   (L429, T217, R1152, B590)
        'SHELLDLL_DefView' 'ShellView' 'ShellViewSHELLDLL_DefView'
DirectUIHWND - ''   (L429, T217, R1152, B590)
        'DirectUIHWND2' 'Files of &type:DirectUIHWND2'
Static - 'File &name:'   (L330, T603, R384, B618)
        'File &name:' 'File &name:Static' 'Static2'
ComboBoxEx32 - ''   (L389, T599, R935, B623)
        'ComboBoxEx' 'File &name:ComboBoxEx'
ComboBox - ''   (L389, T599, R935, B623)
        'ComboBox' 'ComboBox0' 'ComboBox1' 'File &name:ComboBox' 'File &name:ComboBox0' 'File &name:
ComboBox1'
Edit - ''   (L393, T604, R917, B619)
        'Edit' 'File &name:Edit'
Static - 'Files of &type:'   (L200, T152, R200, B152)
        'Files of &type:' 'Files of &type:Static' 'Files of &type:Static0' 'Files of &type:Static1'
'Static3'
ComboBox - 'Media files (all types)'   (L947, T599, R1135, B622)
        'ComboBox2' 'File &name:ComboBox2' 'Files of &type:ComboBox'
Button - '&Open'   (L947, T628, R1035, B654)
        '&Open' '&OpenButton' 'Button' 'Button0' 'Button1'
Button - 'Cancel'   (L1047, T628, R1135, B654)
        'Button2' 'Cancel' 'CancelButton'
ScrollBar - ''   (L1137, T651, R1152, B666)
        'CancelScrollBar' 'ScrollBar3'
WorkerW - ''   (L200, T152, R1152, B187)
        'Files of &type:WorkerW' 'WorkerW'
ReBarWindow32 - ''   (L200, T152, R1152, B187)
        'Files of &type:ReBar' 'ReBar'
TravelBand - ''   (L200, T152, R270, B184)
        'Files of &type:TravelBand' 'TravelBand'
ToolbarWindow32 - ''   (L202, T154, R270, B184)
        'Files of &type:Toolbar' 'Files of &type:Toolbar0' 'Files of &type:Toolbar1' 'Toolbar' 'Tool
bar0' 'Toolbar1'
Address Band Root - ''   (L270, T156, R944, B179)
        'Address Band Root' 'Files of &type:Address Band Root'
msctls_progress32 - ''   (L272, T157, R942, B178)
        'Files of &type:Progress' 'Progress'
Breadcrumb Parent - ''   (L272, T157, R898, B178)
        'Breadcrumb Parent' 'Files of &type:Breadcrumb Parent'
ToolbarWindow32 - 'Address: C:\\Program Files (x86)\\Windows Media Player'   (L272, T157, R898, B178
)
        'Address: C:\\Program Files (x86)\\Windows Media Player' 'Address: C:\\Program Files (x86)\\
Windows Media PlayerToolbar' 'Toolbar2'
ToolbarWindow32 - ''   (L898, T157, R942, B178)
        'Files of &type:Toolbar2' 'Toolbar3'
UniversalSearchBand - ''   (L948, T156, R1152, B179)
        'Files of &type:UniversalSearchBand' 'UniversalSearchBand'
Search Box - ''   (L948, T156, R1152, B179)
        'Files of &type:Search Box' 'Search Box'
SearchEditBoxWrapperClass - ''   (L953, T156, R1147, B179)
        'Files of &type:SearchEditBoxWrapperClass' 'SearchEditBoxWrapperClass'
DirectUIHWND - ''   (L953, T156, R1147, B179)
        'DirectUIHWND3' 'Files of &type:DirectUIHWND3'
>>>
>>> wmp.Open.FilenameEdit.TypeKeys(r"c:\your_file_name.mp3")
<pywinauto.controls.win32_controls.EditWrapper object at 0x027CCC50>
>>> wmp.Open.Open.Click()

您可以(希望)看到 FilenameEdit 来自

Edit - ''   (L393, T604, R917, B619)
        'Edit' 'File &name:Edit'

【讨论】:

    【解决方案2】:

    我可以访问 3ds max 2015 中“选择要导出的文件”中的“文件名”:

    我第一次收到有关“print_control_identifiers()”对话框控件的信息:

    import pywinauto
    from time import sleep
    
     pwa_app = pywinauto.application.Application()
        # pwa_app.start('{app} {path}\{scene}'.format(app=BINARY_PATH, path=TEST_PATH, scene=TEST_SCENE))
    export_dlg_handles = pywinauto.findwindows.find_windows(title=u'Select File to Export', class_name='#32770')
    if export_dlg_handles:
        export_dlg = pwa_app.window_(handle=export_dlg_handles[0])
    export_dlg.print_control_identifiers()
    

    输出:

    Control Identifiers:
    Static - 'Save &in:'   (L172, T146, R258, B159)
            'Save &in:' 'Save &in:Static' 'Static' 'Static0' 'Static1'
    ComboBox - 'export'   (L265, T142, R526, B166)
            'ComboBox' 'ComboBox0' 'ComboBox1' 'Save &in:ComboBox'
    ToolbarWindow32 - ''   (L538, T142, R800, B165)
            ' Toolbar' 'Toolbar' 'Toolbar0' 'Toolbar1'
    ToolbarWindow32 - ''   (L172, T171, R259, B705)
            'Save &in:Toolbar' 'Toolbar2'
    SHELLDLL_DefView - ''   (L265, T171, R857, B650)
            'SHELLDLL_DefView' 'Save &in:SHELLDLL_DefView'
    SysListView32 - 'FolderView'   (L265, T171, R857, B650)
            'FolderView' 'FolderViewListView' 'ListView'
    SysHeader32 - ''   (L265, T171, R857, B190)
            'Header' 'Save &in:Header'
    Static - 'File &name:'   (L267, T664, R354, B677)
            'File &name:' 'File &name:Static' 'Static3'
    ComboBoxEx32 - ''   (L361, T659, R762, B681)
            'ComboBoxEx' 'File &name:ComboBoxEx'
    ComboBox - ''   (L361, T659, R762, B681)
            'ComboBox2' 'File &name:ComboBox'
    Edit - ''   (L365, T664, R742, B677)
            'Edit' 'File &name:Edit'
    Static - 'Save as &type:'   (L267, T690, R354, B703)
            'Save as &type:' 'Save as &type:Static' 'Static4'
    ComboBox - 'Autodesk (*.FBX)'   (L361, T687, R762, B708)
            'ComboBox3' 'Save as &type:ComboBox'
    Button - '&Save'   (L782, T659, R857, B682)
            '&Save' '&SaveButton' 'Button' 'Button0' 'Button1'
    Button - 'Cancel'   (L782, T685, R857, B708)
            'Button2' 'Cancel' 'CancelButton'
    ScrollBar - ''   (L848, T699, R865, B716)
            'CancelScrollBar' 'ScrollBar'
    #32770 - ''   (L166, T102, R865, B716)
            '#32770' 'Save &in:#32770'
    Static - ' '   (L166, T135, R721, B520)
            ' ' ' Static2' 'Static5'
    Static - ''   (L715, T165, R715, B188)
            ' Static3' 'Static6'
    Static - 'Thumbnail'   (L906, T144, R957, B157)
            'Static7' 'Thumbnail' 'ThumbnailStatic'
    ComboBox - 'C:\\Users\\stas\\Documents\\3dsMax\\export'   (L265, T109, R857, B130)
            'ComboBox4' 'History:ComboBox'
    Static - 'History:'   (L219, T113, R255, B126)
            'History:' 'History:Static' 'Static8'
    

    然后我找到一个字符串:

    Edit - ''   (L365, T664, R742, B677)
            'Edit' 'File &name:Edit'
    Button - '&Save'   (L782, T659, R857, B682)
            '&Save' '&SaveButton' 'Button' 'Button0' 'Button1'
    

    “编辑”和“按钮” - 访问控件的名称。

    然后我跑:

    # Endter file name
    export_dlg.Edit.SetText(SAVE_FILE_NAME)
    # Wait one second
    sleep(1.0)
    # Click to "Save" button
    export_dlg.Button.Click()
    

    如需帮助,请查看文档:

    https://pywinauto.googlecode.com/hg/pywinauto/docs/controls_overview.html

    例如所有控件的“SetText”:

    SetText(text, pos_start=None, pos_end=None)
    Set the text of the edit control
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-22
      相关资源
      最近更新 更多