How to Identify Safari Javascript Alert Dialog using AppleScript? In a particularly 'pesky' website, a dialog box pops up to block (or cover) automated entry of the userid input box.

I have used the following code attempting to get a reference &amp; to close this window, but have been unsuccessful. 

tell application "System Events"
    key code 15 using {command down}
    key code 36        // return key

end tell

tell application "System Events" to tell process "Safari"
    set numSheets to (count of sheets) &amp; " sheets" as text       // 0 sheets
end tell

set numDocuments to (count of documents) &amp; " documents" as text  // 1 documents
set numWindows to (count of windows) &amp; " windows" as text        // 1 sheets


What else is available to find and close that alert box?

Edit: The key code 36 will sometimes close the alert, and other times, when it's not present, will submit the page too early.
If you had an alert dialog appearing with an OK button, then this AppleScript code would close it.

tell application "System Events" to tell process "Safari"
    if exists (button "OK" of window 1) then
        click (button "OK" of window 1)
    end if
end tell


You'll likely need to customise this to fit your exact situation. If the alert dialog appears with a title you can all specify that as below.

tell application "System Events" to tell process "Safari"
    if exists (button "OK" of window "Java applet missing") then
        click (button "OK" of window "Java applet missing")
    end if
end tell


Another option would be to identify the alert by using "front window" as in the following example (assuming the alert is the front window).

tell application "System Events" to tell process "Safari"
    if exists (button "OK" of front window) then
        click (button "OK" of front window)
    end if
end tell


Hope this helps.