Search code examples
applescript

Applescript won't click and returns missing value


I'm trying to create a script that will click on my Safari but it doesn't click it. It will just run for a long time then the result will be: missing value. I don't get why it would have a missing value but I just want to click something.

my auto_login_driver()

on auto_login_driver()
    activate application "Safari"
    tell application "System Events"
        
        tell process "Safari"
            set xxx to first UI element
        end tell
        
        click at {1103, 261} -- used command shift 4 to capture the location
    end tell
end auto_login_driver

Solution

  • It won't work. Because the interface of the site only responds to real mouse clicks. To actually click a location on the screen, you will need to use some kind of mouse tool (eg MouseTools). But there is another solution - to use JavaScript. Like here:

    -- SCRIPT: automatically login me to my syte
    -- written by user @Robert Kniazidis
    
    -- EDIT here the URL
    set loginurl to "https:" & "//kinozal-tv.appspot.com/login.php?"
    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
    

    NOTES:

    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.