I have a slightly newer version of the above script.
I made a few changes, but the biggest is adding a repeat loop.
Sometimes when making a new tab in safari, the page isn’t finished fully loaded when the script tries to find the Email & Password fields, so it doesn’t find one or both.
The repeat loop catches this and tries again. EDIT fixed repeat loop so its not infinite
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
-- https://www.dividend.com/login/?redirect_url=/login
property myURL : "https://www.dividend.com/login/?redirect_url=/login"
property eNames : {"Robert", "Mark", "Steven", "Joseph", "Anthony", "Edward", "James", "Ethan", "Henry", "Carlos", "Noah", "Arthur", "Louis", "Zachary", "Thomas", "Alexander", "Jacob", "Vincent", "Dylan", "William", "Jack", "Samuel", "Andrew", "Gabriel", "Benjamin", "Lawrence", "Christopher", "Matthew", "George", "Nicholas", "Daniel", "Nathan", "Peter", "Paul", "Harold", "Adam", "Charles", "Aaron", "Kenneth", "Walter", "Patrick", "Philip", "Austin", "Jesse", "Ryan", "Bruce", "Albert", "Michael", "Eugene", "David", "Eric", "Jonathan", "Frank", "Paul", "Jason", "Brian", "Richard", "Jordan", "Keith", "Kevin", "Russell", "Roger", "Jeremy", "Justin", "Timothy", "Greg", "Alan", "Brandon", "Tyler", "Scott", "Wayne", "Jeffrey", "Dennis", "Douglas", "Roy", "Sean", "Carl", "Ronald", "Donald", "Gerald", "Gary", "Randolph", "Terry"}
on run
local myApp, myTab, myURLs, UIElements, subElements, loginPane, anElement, anItem, i, stack, uname, flag, emailFlag, pwdFlag, tmp
tell application "Safari"
if it is not running then activate it
set tmp to windows
if (count tmp) = 0 then -- Safari is running but has no windows
make new document
set myTab to current tab of window 1
set URL of myTab to myURL
--delay 0.5
else
set myURLs to URL of tabs of window 1
if myURL is in myURLs then -- check to see if one of it's tabs already has the URL
repeat with i from 1 to count myURLs
if (item i of myURLs) = myURL then
exit repeat
end if
end repeat
if current tab of window 1 ≠ tab i of window 1 then set current tab of window 1 to tab i of window 1
else -- no tab or window with URL was found
if (source of current tab of window 1) = "" then -- empty page found, so load URL
set myTab to current tab of window 1
else -- no empty window found, so create a new tab
set myTab to (make new tab at window 1)
set current tab of window 1 to myTab
end if
set URL of myTab to myURL
end if
end if
set flag to true
repeat 50 times
if (name of window 1) = "Login - Dividend.com - Dividend.com" then
set flag to false
exit repeat
end if
delay 0.2
end repeat
end tell
if flag then return false
tell application "System Events"
set myApp to application process "Safari"
repeat 3 times
set emailFlag to false
set pwdFlag to false
set subElements to groups of toolbar 1 of window 1 of myApp -- test if page done loading
repeat with i in subElements -- find group in toolbar that has 'Reload' button
set tmp to contents of i
if exists (button "Add page to Reading List" of tmp) then exit repeat
end repeat
tell tmp
repeat until exists button "Reload this page" --accessibility description
delay 0.2
end repeat
end tell
set flag to false
repeat until flag
try
window 1 of myApp
set anElement to UI element 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 of window 1 of myApp
--get anElement
set flag to true
on error --errMsg number errNum
set anElement to missing value
delay 0.2
end try
if anElement = missing value then set flag to false
end repeat
set flag to false
repeat until flag
try
set subElements to groups of anElement whose subrole is "AXLandmarkMain"
--get subElements
set flag to true
on error --errMsg number errNum
beep 1
end try
if (subElements = missing value) or ((count subElements) = 0) then set flag to false
if flag then
set loginPane to item 1 of subElements
try
set subElements to groups of loginPane whose subrole is not "AXEmptyGroup"
on error
set flag to false
end try
if (count subElements) = 0 then set flag to false
if not flag then tell me to delay 0.2
end if
end repeat
try
repeat with i in subElements -- finds the Email group
set i to contents of i
if (static text 1 of i) exists then
if (value of static text 1 of i) = "Email" then
set anItem to text field 1 of i
set focused of anItem to true
tell me to delay 0.2
set tmp to value of anItem
set uname to tmp
repeat while uname = tmp
set uname to some item of eNames
end repeat
set value of anItem to uname --"Robert"
set emailFlag to true
set focused of anItem to true --text field 1 of i to true -- screen refreshes to remove 'This field is required' notice (if there is one)
tell me to delay 0.2
exit repeat
end if
end if
end repeat
end try
if emailFlag then -- do Password only if Email is done
try
set subElements to groups of loginPane whose subrole is not "AXEmptyGroup"
repeat with i in subElements -- finds the Password group
set i to contents of i
if (static text 1 of i) exists then
if (value of static text 1 of i) = "Password" then
set anItem to text field 1 of i
set focused of anItem to true
tell me to delay 0.2
set value of anItem to my getPWD() --"mypwd"
set pwdFlag to true
exit repeat
end if
end if
end repeat
end try
end if
if pwdFlag then exit repeat -- both fields found, so continue on
end repeat
if pwdFlag then
repeat with i in subElements -- finds the Login button
set i to contents of i
if (button "Log in" of i) exists then
tell button "Log in" of i to perform action "AXPress"
exit repeat
end if
end repeat
end if
end tell
end run
on getPWD()
local pwd, c
set pwd to ""
set c to random number from 6 to 12
repeat with i from 1 to c
set pwd to pwd & some character of "abcdeefghijklmnopqrstuvwxyz!#$%^*-+?~"
end repeat
end getPWD