Running pywinauto using python script through Task scheduler gives the following error. pywinauto version - 0.6.8 Python version - 3.12.0 Windows 10 (Virtual desktop)
Traceback (most recent call last): File "C:\Dashboard_checks\automation\dash_check_final.py", line 144, in sendWebexMessage(arrayStatus) File "C:\Dashboard_checks\automation\dash_check_final.py", line 82, in sendWebexMessage main_dlg.type_keys(msg_text,with_spaces=True,with_newlines=True,pause=0.1,with_tabs=True) File "C:\Users\AppData\Local\Programs\Python\Python312-32\Lib\site-packages\pywinauto\base_wrapper.py", line 964, in type_keys keyboard.send_keys( File "C:\Users\AppData\Local\Programs\Python\Python312-32\Lib\site-packages\pywinauto\keyboard.py", line 713, in send_keys k.run() File "C:\Users\AppData\Local\Programs\Python\Python312-32\Lib\site-packages\pywinauto\keyboard.py", line 411, in run raise RuntimeError('SendInput() inserted only ' + str(num_inserted_events) + RuntimeError: SendInput() inserted only 0 out of 2 keyboard events
app = Application(backend="uia").connect(title_re=".*Webex")
main_dlg = app.top_window()
main_dlg.wait('visible')
def sendWebexMessage(arrayStatus):
date_format = '%Y/%m/%d %H:%M:%S'
dt_format = '%d/%m/%Y %H:%M:%S'
logging.info("sendWebexMessage:START")
main_dlg.set_focus()
#main_dlg.wait('visible')
main_dlg.wait('enabled')
time.sleep(15)
logging.info("sendling Ctrl+Alt+I key")
try:
# sendling Ctrl+Alt+I key.
# set focus on message window of Webex
main_dlg.type_keys('^%I')
except:
logging.warning("sendling Ctrl+Alt+I key:Error")
time.sleep(5)
logging.info("sendling Z and Enter key")
try:
main_dlg.type_keys("Z")
main_dlg.type_keys('{ENTER}')
except:
logging.warning("sending Z and Enter key:Error")
for i in range (0,10):
logging.info("msg_text:"+msg_text)
#print("msg_text:",msg_text)
main_dlg.type_keys(msg_text,with_spaces=True,with_newlines=True,pause=0.1,with_tabs=True)
main_dlg.type_keys('+{ENTER}')
main_dlg.type_keys("\n",with_spaces=True,with_newlines=True,pause=0.1,with_tabs=True)
logging.info("sendWebexMessage:END")
Edit 1: The code works if it is run manually.
Edit 2: The task is being scheduled using the "Task Scheduler" GUI program
I am scheduling the program using a Batch (.bat) file The command in the batch file is as follows:
"C:\Users\AppData\Local\Programs\Python\Python312-32\python.exe" "C:\automation\dash_check_v1.py" > %LOGFILE% 2>&1
If I run the task immediately using the "Run" command within the Task Scheduler GUI the script works without any issue. The issue occurs when I schedule it at a later time.