How to click the OK button in Google Chrome alert, using AppleScript

applescriptgoogle-chrome

I am using AppleScript with Google Chrome to automate a tedious process on a website. In certain conditions, the website has an alert popup to notify the user of an issue. This prevents the rest of the automator script from completing. I need to detect this popup, log it to a file (I already have this figured out), and click the ok button to continue on. The code that I currently have looks like this:

on run args
repeat with catNum in args
    tell application "Google Chrome"
        set myurl to "http://database.com/whatever"
        open location myurl
        delay 1
        tell active tab of window 1
            -- Click the button that needs to be clicked
            execute javascript "document.getElementById('verbatimCoordinatesDiv').getElementsByTagName('div')[0].getElementsByTagName('a')[0].click()"

            -- What I need to do:
            -- If (popup alert) then
                -- do shell script "echo Issue with " & catNum & " > templog.txt"
                -- Click ok button on popup
            -- else
                -- This clicks the save button
                execute javascript "document.getElementById('editButtonDiv').getElementsByTagName('input')[0].click()"
            -- end if

            delay 1
        end tell
        delay 1
        -- Close the tab
        delete tab (active tab index of window 1) of window 1
    end tell
end repeat
end run

So far what I have works fine when there is no popup message, however it requires manual intervention when a popup is triggered. Below is a screenshot of the popup window and Accessibility Inspector window.

Accessibility Inspector Window and Popup Window

EDIT: Here is a stripped down version of what I am trying to do, but on a public site. It appears as though the javascript alert is preventing anything from happening in the browser until OK is manually clicked.

tell application "Google Chrome"
    -- Go to the website with the javascript button
    set myurl to "http://t4t5.github.io/sweetalert/"
    open location myurl
    delay 1
    tell active tab of window 1
        -- Click the normal javascript button on the page
        execute javascript "document.getElementsByTagName('button')[1].click()"
    end tell
    -- Delete the tab when done
    delete tab (active tab index of window 1) of window 1
end tell

Best Answer

You can use a function on window.alert to intercept any alert and to cancel this alert, use a variable to see if there was an alert attempt.

Like this :

tell application "Google Chrome"
    -- Go to the website with the javascript button
    set myurl to "http://t4t5.github.io/sweetalert/"
    open location myurl
    delay 1
    tell active tab of window 1
        -- Click the normal javascript button on the page
        set popupAlert to execute javascript "var isAlert=false; (function() {window.alert = function() {isAlert=true; return;};})(); document.getElementsByTagName('button')[1].click(); isAlert"
        if popupAlert = "true" then -- This page has attempted to display an alert.
            do shell script "echo 'Issue with github.io' > templog.txt"
        else -- no alert, continue
            -- This clicks the save button
        end if
    end tell
    -- Delete the tab when done
    delete (get active tab of window 1)
end tell