Friday, October 22, 2021

#1 2021-08-21 03:56:25 am

From:: Greece
Registered: 2019-03-03
Posts: 2091

Safari: Login to Site Programmatically example.

Exists some sites of my interest, which for one or other reason can't store my account's login settings to the keychain. Let's just say, a very very "bad" site, but with the content I need. And I have to enter the username and password manually every time.

In such cases, following script automatically logs me in. Although nothing prevents you from using this approach for "good" sites too.


-- SCRIPT: automatically login me to my syte

-- EDIT here the URL
set loginurl to "https:" & "//"
set mylogin to "KniazidisR" -- EDIT here account NAME
set myPassword to "mySecretPassword" -- EDIT here the PASSWORD

tell application "Safari" to activate

tell application "Safari" to tell window 1 to tell tab 1
   open location loginurl
   my waitSafariWebPageLoading()
   -- EDIT here the ID of USERNAME html element (I have 'username')
   do JavaScript ("document.getElementById('username').value = '" & mylogin & "'")
   -- EDIT here the ID of PASSWORD html element (I have 'password')
   do JavaScript ("document.getElementById('password').value = '" & myPassword & "'")
   -- EDIT here the SUBMIT BOTTON's CLASS NAME (I have 'buttonS')
   do JavaScript ("document.getElementsByClassName('buttonS')[0].click();")
end tell

on waitSafariWebPageLoading()
   tell application "System Events" to tell application process "Safari"
       repeat until (UI element "Reload this page" of group 3 of toolbar 1 of window 1 exists) or (UI element "Reload this page" of group 2 of toolbar 1 of window 1 exists)
           delay 0.1
       end repeat
   end tell
end waitSafariWebPageLoading

1) JavaScript execution should be allowed in the Safari.
2) To find id of username HTML element, id of password HTML element and class name of submit button, click on them and inspect with Inspect Element of Safari.

Last edited by KniazidisR (2021-08-21 04:44:12 am)

Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)