Search code examples
pythonui-automationpywinauto

How can I extract text from google chrome application using pywinauto?


I'm trying extract list texts on column "Assunto" from windows POP-UP in google chrome using pywinauto.

POP-UP image on google chrome here

    app = Application().connect(title_re='AgênciaNet', timeout=10)
    time.sleep(1)
    window = app.Chrome_RenderWidgetHostHWND
    window.set_focus()
    window.maximize()
    window.print_control_identifiers()

Control Identifiers:

Chrome_WidgetWin_1 - 'AgênciaNet - Secretaria de Economia do Distrito Federal - Google Chrome'    (L-8, T-8, R1032,
B736)
['AgênciaNet - Secretaria de Economia do Distrito Federal - Google ChromeChrome_WidgetWin_1', 'Chrome_WidgetWin_1',
'AgênciaNet - Secretaria de Economia do Distrito Federal - Google Chrome']
child_window(title="AgênciaNet - Secretaria de Economia do Distrito Federal - Google Chrome", class_name="Chrome_WidgetWin_1")
   |
   | Chrome_RenderWidgetHostHWND - 'Chrome Legacy Window'    (L0, T71, R1024, B728)
   | ['Chrome Legacy Window', 'Chrome Legacy WindowChrome_RenderWidgetHostHWND', 'Chrome_RenderWidgetHostHWND', 'Chrome Legacy Window0', 'Chrome Legacy Window1', 'Chrome Legacy WindowChrome_RenderWidgetHostHWND0', 'Chrome Legacy WindowChrome_RenderWidgetHostHWND1', 'Chrome_RenderWidgetHostHWND0', 'Chrome_RenderWidgetHostHWND1']
   | child_window(title="Chrome Legacy Window", class_name="Chrome_RenderWidgetHostHWND")
   |
   | Chrome_RenderWidgetHostHWND - 'Chrome Legacy Window'    (L0, T0, R1024, B657)
   | ['Chrome Legacy Window2', 'Chrome Legacy WindowChrome_RenderWidgetHostHWND2', 'Chrome_RenderWidgetHostHWND2']
   | child_window(title="Chrome Legacy Window", class_name="Chrome_RenderWidgetHostHWND")
   |
   | Intermediate D3D Window - ''    (L0, T0, R1024, B728)
   | ['AgênciaNet - Secretaria de Economia do Distrito Federal - Google ChromeIntermediate D3D Window', 'Intermediate D3D Window']
   | child_window(class_name="Intermediate D3D Window")

My inspecter

I need get tag "Name". Anyone can help me please. Ty!

Specifications:

  • Pywinauto 0.6.8
  • Python 3.10.7
  • Windows 10

Solution

  • First please make sure your target window has the same ProcessId in Inspect.exe. If the process is different than for Chrome window, you need to connect or use something like:

    from pywinauto import Desktop
    
    window = Desktop(backend="uia").window(title="Selecione um certificado")
    window.child_window(title_re="MERCADAO COMERCIO.*", control_type="DataItem").click_input()
    window.child_window(title="OK", control_type="Button").invoke()
    

    For better understanding how it works I would recommend reading the Getting Started Guide.

    If this is a self-drawn "window" on the page, there is trick for Chrome: https://github.com/pywinauto/pywinauto/wiki/How-to-enable-accessibility-(tips-and-tricks)

    Or use this example: https://github.com/vasily-v-ryabov/ui-automation-course/blob/master/02_google_drive_Murashov/test_explorer_google_drive.py