Pyautogui not entering all characters when using typewrite function we are trying to automate the SecurityAgent dialog where we are inputting password using pyautogui in 10.13 OSx

password = "12345678"
using 

pyautogui.typewrite(password)


this function solved the problem. However, we are running this script many number of times in the same machine

Am not sure if it is because of the machine slowness or something , only 7 characters are getting written on the screen and enter is clicked which fails the step.

i also tried adding interval and pause value in pyautogui. But it doesnt help. Randomly script is failing. 

Instead of typing a whole sequence i tried to run a loop like below 

for c in password:
  pyautogui.press(c)


Still it didnt help. Can someone help with the alternate solution ?
With AppleScript I made this for use in Automator:

https://gist.github.com/SuperHofstad/f826985ec536ff2d650699cb37a7fadc

set appPass to "PASSWORD" as text

tell application "System Events"
    delay 0.5
    repeat until exists process "SecurityAgent"
        delay 0.5
    end repeat
    tell process "SecurityAgent"
        keystroke appPass
        key code 36
    end tell
end tell