I have that setting turned on and have run the script multiple times and it works every time. I also tried it by stepping through and it still worked every time.
set increment to random number from 1 to 10
set myUserName to "test " & increment as text
activate application "Safari"
tell application "System Events"
tell process "Safari"
keystroke "l" using {command down} -- shifts focus to URL field
delay 1
keystroke myUserName
end tell
end tell
→ Script Debugger 8.0.3 (8A49)
→ Mac OS 11.6.1 (20G224)
I’m curious why you want to use GUI scripting to enter text in the URL field.
Is there a reason you can’t do something like this:
set myURL to "https://forum.latenightsw.com/t/applescript-to-enter-a-webpage-login-credentials/3482/21"
tell application "Safari"
set URL of current tab of window 1 to myURL
end tell
Joel,
Thanks for your work on developing this applescript. It gave me a few important clues about how I could do that on a web-site into which I have to login each day to get my electricity usage for the previous day.
I have got that working now essentially using your code to enter username and password with some added delays. My only issue now is that Safari throws up a dialog box wanting to use a saved login for that site which I will have to work out how to dismiss.
It also took me a long time to actually work out how to get down to the right GUIScripting elements as the web-site is basically javascript, but I found some code from Yvan Koenig which helped me discover the element tree I needed to follow.
I hope you have managed to get your code working and I can share mine if you would like to view it.
joel, here is where I have got to on the login. Unfortunately an AutoFill popup appears after I have logged in and reached the ‘Home’ page of the website. I still need to work out how to Cancel that.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Safari"
set myUserName to "username"
set myPass to "apassword" & return
set powerTab to "Energy"
# Make the citipower tab the current one and force a reload
set winList to index of every window
repeat with winIndex in winList
try
tell window winIndex
set current tab to (first tab whose name contains powerTab)
end tell
end try
end repeat
tell application "System Events" to tell application process "Safari"
activate
# This does not clear the text fields like hitting the '^(_)' reload button in url field
click menu item "Reload Page" of menu 1 of menu bar item "View" of menu bar 1
set frontmost to true
delay 1
# This UIElement string is from my SafariUI.scpt with "myEnergy" window selected
tell window 1 to tell splitter group 1 to tell tab group 1 to tell group 1 to tell group 1 to tell scroll area 1 to tell UI element 1
tell group 7
# following approach is from https://forum.latenightsw.com/t/applescript-to-enter-a-webpage-login-credentials/3482/13
tell text field 1
repeat until exists of it
delay 0.25
end repeat
set its focused to true
set its value to myUserName
end tell
delay 0.5
end tell
tell group 9
tell text field 1
set its focused to true
keystroke myPass as text
end tell
delay 0.5
end tell
tell group 10
perform action "AXPress" of button "Log in"
end tell
end tell
end tell
end tell