Pywinauto: wait Operation error

Created on 8 Jan 2019  路  14Comments  路  Source: pywinauto/pywinauto

Hi Vasily,

I am trying to access outlook.

Based on docs and examples, I am trying below code with wait operation and getting timeout error before application open and visible. What might be best way to use wait operation?

from pywinauto.application import Application 
app = Application().start(r'C:\Program Files (x86)\Microsoft Office\root\Office16\OUTLOOK.EXE')
app.rctrl_renwnd32.wait('enabled', timeout = 20) 


Traceback (most recent call last):
  File "C:/Users/rrmamidi/Desktop/old Desktop/compress_1/python/basic python scripts/attache_mail.py", line 9, in <module>
    app.rctrl_renwnd32.wait('enabled', timeout = 20)
  File "C:\Users\rrmamidi\AppData\Local\Programs\Python\Python36\lib\site-packages\pywinauto-0.6.5-py3.6.egg\pywinauto\application.py", line 502, in wait
    lambda: self.__check_all_conditions(check_method_names, retry_interval))
  File "C:\Users\rrmamidi\AppData\Local\Programs\Python\Python36\lib\site-packages\pywinauto-0.6.5-py3.6.egg\pywinauto\timings.py", line 370, in wait_until
    raise err
pywinauto.timings.TimeoutError: timed out

Thanks,
Raja

need investigation question

All 14 comments

Hi @rajarameshmamidi the wait method call is correct, but it looks like you need Application(backend="uia") because default backend is "win32" and many Outlook elements won't be visible to pywinauto (and Spy++). Just in case: Getting Started Guide is the first document to read before "Waiting for long operations" chapter.

Method app.windows() may help to check titles (method .window_text()) of top level windows of the app.

Hi Vasily,

i gone through the guide and
i tried as below but not working...

import time
from pywinauto.application import Application 
app = Application(backend="uia").start(r'C:\Program Files (x86)\Microsoft Office\root\Office16\OUTLOOK.EXE')
app.rctrl_renwnd32.wait('active', timeout =10)

i have attached the controls of outlook and image which i am trying to ....for now i need to click on New Email button and attache a doc to that mail...this is what i am trying too.. sample code will be helpfull..

controls.docx

outlook

Thanks,
raja

Well, the title of main window is obviously incorrect. Just run print app.windows() to see correct title. This code waits successfully:

from pywinauto.application import Application 
app = Application(backend="uia").start(r'C:\Program Files (x86)\Microsoft Office\root\Office16\OUTLOOK.EXE')
app.window(title_re='Inbox - .* - Outlook').wait('ready', timeout=10)
app.window(title_re='Inbox - .* - Outlook').dump_tree()

In the output of dump_tree() you will find children elements of the main window. It will be the second level of window specification. It's easy to copy-paste correct element description: child_window(title="New Email", control_type="Button") so further code should look so:

app.window(title_re='Inbox - .* - Outlook').child_window(title="New Email", control_type="Button").click()

Then you will see that New Email window is a top level window again:

>>> app.windows()
[<uiawrapper.UIAWrapper - 'Untitled - Message (HTML) ', Dialog, -88330517>,
 <uiawrapper.UIAWrapper - '', Pane, -1967265827>,
 <uiawrapper.UIAWrapper - '', Pane, -1984586227>,
 <uiawrapper.UIAWrapper - '', Pane, -1982421177>,
 <uiawrapper.UIAWrapper - '', Pane, 388596319>,
 <uiawrapper.UIAWrapper - 'Inbox - ### - Outlook', Dialog, 1139580923>]

So the next dump_tree() call will help you with further steps:

app.window(title="Untitled - Message (HTML) ", control_type="Window").dump_tree()

Hi Vasily,

i am getting below error after using above code while generating controls, Any reason why below error ?

Traceback (most recent call last):
  File "C:/Users/rrmamidi/Desktop/old Desktop/compress_1/python/basic python scripts/attache_mail.py", line 5, in <module>
    app.window(title_re='Inbox - mailid - Outlook').dump_tree() # shows controls of the window
  File "C:\Users\rrmamidi\AppData\Local\Programs\Python\Python36\lib\site-packages\pywinauto-0.6.5-py3.6.egg\pywinauto\application.py", line 661, in print_control_identifiers
    print_identifiers([this_ctrl, ])
  File "C:\Users\rrmamidi\AppData\Local\Programs\Python\Python36\lib\site-packages\pywinauto-0.6.5-py3.6.egg\pywinauto\application.py", line 657, in print_identifiers
    print_identifiers(ctrl.children(), current_depth + 1, log_func)
  File "C:\Users\rrmamidi\AppData\Local\Programs\Python\Python36\lib\site-packages\pywinauto-0.6.5-py3.6.egg\pywinauto\application.py", line 657, in print_identifiers
    print_identifiers(ctrl.children(), current_depth + 1, log_func)
  File "C:\Users\rrmamidi\AppData\Local\Programs\Python\Python36\lib\site-packages\pywinauto-0.6.5-py3.6.egg\pywinauto\application.py", line 657, in print_identifiers
    print_identifiers(ctrl.children(), current_depth + 1, log_func)
  File "C:\Users\rrmamidi\AppData\Local\Programs\Python\Python36\lib\site-packages\pywinauto-0.6.5-py3.6.egg\pywinauto\application.py", line 657, in print_identifiers
    print_identifiers(ctrl.children(), current_depth + 1, log_func)
  File "C:\Users\rrmamidi\AppData\Local\Programs\Python\Python36\lib\site-packages\pywinauto-0.6.5-py3.6.egg\pywinauto\application.py", line 657, in print_identifiers
    print_identifiers(ctrl.children(), current_depth + 1, log_func)
  File "C:\Users\rrmamidi\AppData\Local\Programs\Python\Python36\lib\site-packages\pywinauto-0.6.5-py3.6.egg\pywinauto\application.py", line 615, in print_identifiers
    ctrl_id = all_ctrls.index(ctrl)
  File "C:\Users\rrmamidi\AppData\Local\Programs\Python\Python36\lib\site-packages\pywinauto-0.6.5-py3.6.egg\pywinauto\base_wrapper.py", line 608, in __eq__
    return self.element_info == other.element_info
  File "C:\Users\rrmamidi\AppData\Local\Programs\Python\Python36\lib\site-packages\pywinauto-0.6.5-py3.6.egg\pywinauto\uia_element_info.py", line 343, in __eq__
    return bool(IUIA().iuia.CompareElements(self.element, other.element))
_ctypes.COMError: (-2147220991, 'An event was unable to invoke any of the subscribers', (None, None, None, 0, None))

Thanks,
Raja

Hi @rajarameshmamidi can you reproduce the same error with current master branch? It can be installed so:

pip uninstall pywinauto
pip install https://github.com/pywinauto/pywinauto/archive/master.zip

Hi Vasily,

i performed above 2 steps and ran the code again and got below error,

Traceback (most recent call last):
  File "C:/Users/rrmamidi/Desktop/old Desktop/compress_1/python/basic python scripts/attache_mail.py", line 7, in <module>
    app.window(title_re='Inbox - mailid - Outlook').child_window(title="New Email", control_type="Button").click()
  File "C:\Users\rrmamidi\AppData\Local\Programs\Python\Python36\Lib\site-packages\pywinauto\controls\uia_controls.py", line 119, in click
    self.invoke()
  File "C:\Users\rrmamidi\AppData\Local\Programs\Python\Python36\Lib\site-packages\pywinauto\controls\uiawrapper.py", line 513, in invoke
    self.iface_invoke.Invoke()
_ctypes.COMError: (-2147467259, 'Unspecified error', (None, None, None, 0, None))

and most of times below error:

Traceback (most recent call last):
  File "C:/Users/rrmamidi/Desktop/old Desktop/compress_1/python/basic python scripts/attache_mail.py", line 5, in <module>
    app.window(title_re='Inbox - mailid - Outlook').wait('ready', timeout=10)
  File "C:\Users\rrmamidi\AppData\Local\Programs\Python\Python36\Lib\site-packages\pywinauto\application.py", line 512, in wait
    lambda: self.__check_all_conditions(check_method_names, retry_interval))
  File "C:\Users\rrmamidi\AppData\Local\Programs\Python\Python36\Lib\site-packages\pywinauto\timings.py", line 375, in wait_until
    raise err
pywinauto.timings.TimeoutError: timed out

Thanks,
Raja

For the second error it looks like timeout 10 seconds is insufficient. Use .wait('ready', timeout=30) for example.

For the first error I need more details: Python version + 32/64-bit of Python, Windows 32/64-bit?

Hi Vasily,

i have installed 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] of python and windows7 64-bit operating system.

Thanks,
Raja

Hi Vasily,

could please share an update on this request...

Thanks,
raja

@rajarameshmamidi, as you can see the issue has been labeled to be fixed only in the next release of pywinauto 0.7 Any issues that need a deeper investigation are usually postponed as we try to focus on upcoming release 0.6.6

BTW: I noticed in your doc a warning about trying to automate 32-bit application with 64-bit version of python. You may consider giving a try to 32-bit Python.

Hi Airelil / Vasily,

now i am able to get controls of outlook -64-bit and getting element not found error with below codes,

Below code is to click insert of an Untitled - Message (HTML)

app.window(title_re="Untitled - Message (HTML)", control_type="Window").child_window(title="Insert", control_type="TabItem").click()

Below code is to click Attache File of an Untitled - Message (HTML)

app.window(title_re="Untitled - Message (HTML)", control_type="Window").child_window(title="Message", control_type="Group").child_window(title="Attach File...", control_type="MenuItem").click()
Traceback (most recent call last):
  File "c:/Users/rrmamidi/Desktop/old Desktop/compress_1/python/basic python scripts/about tikinter/attache_mail.py", line 15, in <module>
    app.window(title_re="Untitled - Message (HTML)", control_type="Window").child_window(title="Message", control_type="Group").child_window(title="Attach File...", control_type="MenuItem").click()
  File "C:\Users\rrmamidi\AppData\Local\Programs\Python\Python36\Lib\site-packages\pywinauto\application.py", line 362, in __getattribute__
    ctrls = self.__resolve_control(self.criteria)
  File "C:\Users\rrmamidi\AppData\Local\Programs\Python\Python36\Lib\site-packages\pywinauto\application.py", line 259, in __resolve_control
    raise e.original_exception
  File "C:\Users\rrmamidi\AppData\Local\Programs\Python\Python36\Lib\site-packages\pywinauto\timings.py", line 458, in wait_until_passes
    func_val = func(*args, **kwargs)
  File "C:\Users\rrmamidi\AppData\Local\Programs\Python\Python36\Lib\site-packages\pywinauto\application.py", line 201, in __get_ctrl
    dialog = self.backend.generic_wrapper_class(findwindows.find_element(**criteria[0]))
  File "C:\Users\rrmamidi\AppData\Local\Programs\Python\Python36\Lib\site-packages\pywinauto\findwindows.py", line 87, in find_element
    raise ElementNotFoundError(kwargs)
pywinauto.findwindows.ElementNotFoundError: {'title_re': 'Untitled - Message (HTML)', 'control_type': 'Window', 'backend': 'uia', 'process': 13876}

new mail controlls.docx

Thanks,
Raja

Hi @rajarameshmamidi, just a side note. Did you consider using other ways to automate your task? For example, MS Outlook can be automated through COM interface. I was able to google several examples just in no time.
You can use VBA as described here:
https://support.microsoft.com/en-nz/help/209948/how-to-use-automation-to-send-a-microsoft-outlook-message-using-access
Or have a look at this article, if you prefer stick to Python:
https://pbpython.com/windows-com.html

Here a short example heavily based on the above example:

import win32com.client as win32
from pathlib import Path

# Open up an outlook email
outlook = win32.gencache.EnsureDispatch('Outlook.Application')
new_mail = outlook.CreateItem(0)

# Label the subject
new_mail.Subject = "Test Outlook COM automation"

# Add the to and cc list
new_mail.To = "[email protected]"

with open("test.txt", mode="w") as f:
    f.write("Test attachment")

out_file = Path.cwd() / "test.txt"

# The file needs to be a string not a path object
new_mail.Attachments.Add(Source=str(out_file))

# Display the email
new_mail.Display(True)

You can use new_mail.Send() method to actually send the mail

Thank you very much Airelil. it was helpful.

Hi @rajarameshmamidi

Were you able to find the cause of that error "pywinauto.findwindows.ElementNotFoundError" ?

or @vasily-v-ryabov / @airelil would you have any suggestions?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MyerFire picture MyerFire  路  37Comments

yangliang003 picture yangliang003  路  18Comments

harryhobson picture harryhobson  路  13Comments

vasily-v-ryabov picture vasily-v-ryabov  路  36Comments

Nonisiuniis picture Nonisiuniis  路  25Comments