Entering Web Page Login Credentials In Safari

I wrote a script to open a web page and enter the login credentials but am stuck in getting the login credentials code to work.

The relevant part of the script is as follows:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Safari"
	
	set myUserName to "abcdefgh"
	set myPassword to "ijklmnopq"
	
	set tabList to index of every window
	set tabName to "Scotiabank"
	repeat with i in tabList
		try
			tell window i
				set current tab to (first tab whose name contains tabName)
			end tell
		end try
	end repeat
	
	tell application "System Events" to tell application process "Safari"
		delay 2
		keystroke tab
		keystroke myUserName
		keystroke tab
		keystroke myPassword
		keystroke return
	end tell
end tell


The script fails when executing the keystroke tab command and produces the following error message:

https://www.dropbox.com/s/mp3zwsdwpmq4i4i/20211211_safari_credential%20entry.png?dl=0

I would appreciate assistance in getting this to work as I am seriously stuck.

Thank you.

Very often, you can log into a site without GUI scripting. Here’s an example: Safari: login to Site Programatically example..

But a lot depends on the specifics of the HTML of your site. Therefore, if you want to get professional help, then publish here, if possible, the URL of your site.