Skip to content Skip to sidebar Skip to footer

Cannot Handle Pywinauto Related Exceptions With `try Except` Block In Python Properly?

My main goal: Automating Zoom my zoomModule.py file is: import os, pywinauto from pywinauto import Desktop from pywinauto import findwindows from pywinauto.application import Appli

Solution 1:

Seems like I have figured out how to print() the errors raised by pywinauto to the console/terminal without crashing my code with the help of this answer.

open_join_meeting_window function - Before ↓↓↓

# after importsdefopen_join_meeting_window():
    """opens the Join Meeting Popup Window; returns the Join Meeting Window"""
    zoomMainWin = get_Zoom_Main_Window()
    zoomMainWin["Zoom"].child_window(
        title="Home", control_type="TabItem"
    ).wrapper_object().click_input()
    zoomMainWin["Zoom"].child_window(
        title="Join", control_type="Button"
    ).wrapper_object().click_input()
    try:
        joinMeetingWin = app.connect(title="Join Meeting", timeout=15)
        print("Connected to Join Meeting Window.")
    except (findwindows.ElementNotFoundError, pywinauto.timings.TimeoutError) as e:
        print("Err before joinMeetingWin: ", e)
        pass

open_join_meeting_window function - after modification ↓↓↓

defopen_join_meeting_window():
    """opens the Join Meeting Popup Window; returns the Join Meeting Window"""
    zoomMainWin = get_Zoom_Main_Window()
    zoomMainWin["Zoom"].child_window(
        title="Home", control_type="TabItem"
    ).wrapper_object().click_input()
    zoomMainWin["Zoom"].child_window(
        title="Join", control_type="Button"
    ).wrapper_object().click_input()
    try:
        joinMeetingWin = app.connect(title="Join Meeting", timeout=15)
        print("Connected to Join Meeting Window.")
    except Exception as e:
        logging.error(traceback.format_exc())
        if e.__class__.__name__ == "TimeoutError":
            print("[TimeoutError]:- The `Join Meeting Window` is taking longer time to appear.")
    print("Passed the try-except block!!")
        pass

New terminal logs↓↓↓

ERROR:root:Traceback (most recent call last):
  File"C:\Users\RAKTIM BHATTACHARYA.000\AppData\Local\Programs\Python\Python39\lib\site-packages\pywinauto\timings.py", line 436, in wait_until_passes
    func_val = func(*args, **kwargs)
  File"C:\Users\RAKTIM BHATTACHARYA.000\AppData\Local\Programs\Python\Python39\lib\site-packages\pywinauto\findwindows.py", line 87, in find_element  
    raise ElementNotFoundError(kwargs)
pywinauto.findwindows.ElementNotFoundError: {'title': 'hhh', 'backend': 'uia', 'visible_only': False}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File"D:\PythonDevelopment\Lake\zoom\getJoinMeetingWindow.py", line 14, in open_join_meeting_window
    joinMeetingWin = app.connect(title="hhh", timeout=6)
  File"C:\Users\RAKTIM BHATTACHARYA.000\AppData\Local\Programs\Python\Python39\lib\site-packages\pywinauto\application.py", line 990, in connect      
    self.process = timings.wait_until_passes(
  File"C:\Users\RAKTIM BHATTACHARYA.000\AppData\Local\Programs\Python\Python39\lib\site-packages\pywinauto\timings.py", line 458, in wait_until_passes
    raise err
pywinauto.timings.TimeoutError

[TimeoutError]:- The`Join Meeting Window` is taking longer time to appear.
Passed the try-except block!!

First, the traditional traceback is printed. Then the name of the error raised by pywinauto is used in the control-flow to decide what to do.

Solution 2:

Change this :

joinMeetingWin = app.connect(title="Join Meeting", timeout=6)

by this :

joinMeetingWin = app.connect(title="Join", timeout=6)

Post a Comment for "Cannot Handle Pywinauto Related Exceptions With `try Except` Block In Python Properly?"