【问题标题】:Reading datagridview (table) using pywinauto使用 pywinauto 读取 datagridview (table)
【发布时间】:2018-03-02 11:55:22
【问题描述】:

我想使用 pywinauto 进行一些自动化操作,但我一直在阅读 DataGridView 表。如何将其转换为可读表格?我需要查看表中的实际数据。表中的列是动态的,您可以添加或删除您需要的列。据我了解,我需要使用 ListViewWrapper 转换 ir,但我无法获取表格的元素。控件标识符:

app = Application(backend='uia').connect(title_re=app_regex)
dgv = app.top_window().DataGridView
dgv.print_control_identifiers()

Table - 'DataGridView'    (L198, T191, R1366, B621)
['DataGridViewTable', 'Table', 'DataGridView']
child_window(title="DataGridView", auto_id="dgvBrowser", control_type="Table")
   | 
   | ScrollBar - 'Vertical Scroll Bar'    (L1348, T192, R1365, B620)
   | ['ScrollBar', 'Vertical Scroll BarScrollBar', 'Vertical Scroll Bar']
   | child_window(title="Vertical Scroll Bar", auto_id="4261242", control_type="ScrollBar")
   |    | 
   |    | Button - 'Line up'    (L1348, T192, R1365, B209)
   |    | ['Button', 'Line up', 'Line upButton', 'Button0', 'Button1']
   |    | child_window(title="Line up", control_type="Button")
   |    | 
   |    | Thumb - 'Position'    (L1348, T209, R1365, B590)
   |    | ['Position', 'PositionThumb', 'Thumb']
   |    | child_window(title="Position", control_type="Thumb")
   |    | 
   |    | Button - 'Page down'    (L1348, T590, R1365, B603)
   |    | ['Button2', 'Page down', 'Page downButton']
   |    | child_window(title="Page down", control_type="Button")
   |    | 
   |    | Button - 'Line down'    (L1348, T603, R1365, B620)
   |    | ['Button3', 'Line down', 'Line downButton']
   |    | child_window(title="Line down", control_type="Button")
   | 
   | Custom - 'Top Row'    (L198, T191, R1348, B213)
   | ['Custom', 'Top RowCustom', 'Top Row', 'Custom0', 'Custom1']
   | child_window(title="Top Row", control_type="Custom")
   |    | 
   |    | Header - ''    (L199, T191, R239, B213)
   |    | ['', 'Header', '0', '1', 'Header0', 'Header1']
   |    | 
   |    | Header - ''    (L239, T191, R264, B213)
   |    | ['2', 'Header2']
   |    | 
   |    | Header - 'Data'    (L264, T191, R352, B213)
   |    | ['DataHeader', 'Header3', 'Data']
   |    | child_window(title="Data", control_type="Header")
   |    | 
   |    | Header - 'Technikas'    (L352, T191, R484, B213)
   |    | ['TechnikasHeader', 'Technikas', 'Header4']
   |    | child_window(title="Technikas", control_type="Header")
   |    | 
   |    | Header - 'Paskyros numeris'    (L484, T191, R638, B213)
   |    | ['Paskyros numerisHeader', 'Header5', 'Paskyros numeris']
   |    | child_window(title="Paskyros numeris", control_type="Header")
   |    | 
   |    | Header - 'Objektas-Adresas'    (L638, T191, R762, B213)
   |    | ['Objektas-Adresas', 'Objektas-AdresasHeader', 'Header6']
   |    | child_window(title="Objektas-Adresas", control_type="Header")
   |    | 
   |    | Header - 'Klientas'    (L762, T191, R866, B213)
   |    | ['Klientas', 'Header7', 'KlientasHeader']
   |    | child_window(title="Klientas", control_type="Header")
   |    | 
   |    | Header - 'Stadija'    (L866, T191, R997, B213)
   |    | ['StadijaHeader', 'Header8', 'Stadija']
   |    | child_window(title="Stadija", control_type="Header")
   |    | 
   |    | Header - 'Darbu vykdytojas'    (L997, T191, R1147, B213)
   |    | ['Darbu vykdytojasHeader', 'Darbu vykdytojas', 'Header9']
   |    | child_window(title="Darbu vykdytojas", control_type="Header")
   |    | 
   |    | Header - 'Projektas'    (L1147, T191, R1348, B213)
   |    | ['ProjektasHeader', 'Header10', 'Projektas']
   |    | child_window(title="Projektas", control_type="Header")
   | 
   | Custom - 'Row 0'    (L199, T213, R1348, B234)
   | ['Custom2', 'Row 0Custom', 'Row 0']
   | child_window(title="Row 0", control_type="Custom")
   |    | 
   |    | Custom - ' Row 0'    (L199, T213, R239, B234)
   |    | ['Custom3', ' Row 0', ' Row 0Custom', ' Row 00', ' Row 01', ' Row 0Custom0', ' Row 0Custom1']
   |    | child_window(title=" Row 0", control_type="Custom")
   |    | 
   |    | Custom - ' Row 0'    (L239, T213, R264, B234)
   |    | ['Custom4', ' Row 02', ' Row 0Custom2']
   |    | child_window(title=" Row 0", control_type="Custom")
   |    | 
   |    | Custom - 'Data Row 0'    (L264, T213, R352, B234)
   |    | ['Custom5', 'Data Row 0Custom', 'Data Row 0']
   |    | child_window(title="Data Row 0", control_type="Custom")
   |    | 
   |    | Custom - 'Technikas Row 0'    (L352, T213, R484, B234)
   |    | ['Custom6', 'Technikas Row 0', 'Technikas Row 0Custom']
   |    | child_window(title="Technikas Row 0", control_type="Custom")
   |    | 
   |    | Custom - 'Paskyros numeris Row 0'    (L484, T213, R638, B234)
   |    | ['Custom7', 'Paskyros numeris Row 0Custom', 'Paskyros numeris Row 0']
   |    | child_window(title="Paskyros numeris Row 0", control_type="Custom")
   |    | 
   |    | Custom - 'Objektas-Adresas Row 0'    (L638, T213, R762, B234)
   |    | ['Custom8', 'Objektas-Adresas Row 0', 'Objektas-Adresas Row 0Custom']
   |    | child_window(title="Objektas-Adresas Row 0", control_type="Custom")
   |    | 
   |    | Custom - 'Klientas Row 0'    (L762, T213, R866, B234)
   |    | ['Custom9', 'Klientas Row 0Custom', 'Klientas Row 0']
   |    | child_window(title="Klientas Row 0", control_type="Custom")
   |    | 
   |    | Custom - 'Stadija Row 0'    (L866, T213, R997, B234)
   |    | ['Custom10', 'Stadija Row 0', 'Stadija Row 0Custom']
   |    | child_window(title="Stadija Row 0", control_type="Custom")
   |    | 
   |    | Custom - 'Darbu vykdytojas Row 0'    (L997, T213, R1147, B234)
   |    | ['Custom11', 'Darbu vykdytojas Row 0', 'Darbu vykdytojas Row 0Custom']
   |    | child_window(title="Darbu vykdytojas Row 0", control_type="Custom")
   |    | 
   |    | Custom - 'Projektas Row 0'    (L1147, T213, R1348, B234)
   |    | ['Custom12', 'Projektas Row 0', 'Projektas Row 0Custom']
   |    | child_window(title="Projektas Row 0", control_type="Custom")
   | 
   | Custom - 'Row 1'    (L199, T234, R1348, B255)
   | ['Custom13', 'Row 1', 'Row 1Custom']
   | child_window(title="Row 1", control_type="Custom")
   |    | 
   |    | Custom - ' Row 1'    (L199, T234, R239, B255)
   |    | ['Custom14', ' Row 1Custom', ' Row 1', ' Row 1Custom0', ' Row 1Custom1', ' Row 10', ' Row 11', ' Row 100', ' Row 101', ' Row 110', ' Row 111']
   |    | child_window(title=" Row 1", control_type="Custom")
   |    | 
   |    | Custom - ' Row 1'    (L239, T234, R264, B255)
   |    | ['Custom15', ' Row 1Custom2', ' Row 12', ' Row 120', ' Row 121']
   |    | child_window(title=" Row 1", control_type="Custom")
   |    | 
   |    | Custom - 'Data Row 1'    (L264, T234, R352, B255)
   |    | ['Custom16', 'Data Row 1Custom', 'Data Row 1']
   |    | child_window(title="Data Row 1", control_type="Custom")
   |    | 
   |    | Custom - 'Technikas Row 1'    (L352, T234, R484, B255)
   |    | ['Custom17', 'Technikas Row 1Custom', 'Technikas Row 1']
   |    | child_window(title="Technikas Row 1", control_type="Custom")
   |    | 
   |    | Custom - 'Paskyros numeris Row 1'    (L484, T234, R638, B255)
   |    | ['Custom18', 'Paskyros numeris Row 1', 'Paskyros numeris Row 1Custom']
   |    | child_window(title="Paskyros numeris Row 1", control_type="Custom")

谢谢

【问题讨论】:

  • “可读表”是什么意思?您期望哪种格式的表格?包含所有单元格文本的列表列表?
  • 我的意思是我想读取单元格的值。
  • 从输出中我看到每个单元格都有自定义类型(因此 GridWrapper 方法不适用),但文本可用。 'Technikas Row 1'等是你需要的吗?
  • 'Technikas' 是列名,'row 1' 是行号。实际值不同。

标签: python python-3.x automation ui-automation pywinauto


【解决方案1】:

您可能不喜欢这个答案,但目前的情况不适合您的任务。

  1. 如果您无权访问应用程序源代码,则无法使用 pywinauto 获取文本。

  2. 但如果您有,则需要为此控件类型实现所谓的服务器提供程序(在 MSDN 上的 UI 自动化 API 文档中描述)。虽然我从未尝试过这种方式。

  3. 另一种理论上可行的方法应该使用托管 DLL 注入来访问自定义 .NET 控件的内部属性。我知道 TestComplete、Squish 和 UFT 使用这种方式。但它们都非常昂贵。从未听说过使用这种方法的任何开源工具。

附:屏幕文字识别理论上也是可行的方式,但工具不够成熟,无法可靠识别。

【讨论】:

  • 今天我使用 UIpath rpa 工具进行了测试,它可以识别单元格中的文本。是因为那个工具是用 c# 构建的吗?
  • 这也是商业工具。很可能它使用 DLL 注入来获取 .NET 控件的内部属性。 C# 没那么重要。我们正在对 DLL 注入方法进行初步研究。在 Python 中也应该可以。但是很难指定截止日期,因为 pywinauto 是我们团队所有成员的爱好项目。
  • 好的,我明白了,谢谢。我真的很喜欢 python 和 pywinauto,我希望它会越来越大:)
  • P.S 当我在我的个人 Windows 10 笔记本电脑上打印控件时,单元格控件类型是 DataItem
  • 嗯...那么它可以使用标准方法在 Win10 上运行。 :)
猜你喜欢
  • 2020-09-29
  • 2016-05-17
  • 1970-01-01
  • 2014-04-05
  • 1970-01-01
  • 1970-01-01
  • 2021-08-03
  • 2012-06-22
  • 2011-06-24
相关资源
最近更新 更多