Ich verwende AppleScript mit Google Chrome, um einen langwierigen Prozess auf einer Website zu automatisieren. Unter bestimmten Bedingungen verfügt die Website über ein Warn-Popup, um den Benutzer über ein Problem zu informieren. Dadurch wird verhindert, dass der Rest des Automator-Skripts abgeschlossen wird. Ich muss dieses Popup erkennen, es in einer Datei protokollieren (ich habe das bereits herausgefunden) und auf die Schaltfläche „OK“ klicken, um fortzufahren. Der Code, den ich derzeit habe, sieht so aus:
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
Bisher funktioniert das, was ich habe, einwandfrei, wenn keine Popup-Meldung angezeigt wird. Es erfordert jedoch einen manuellen Eingriff, wenn ein Popup ausgelöst wird. Unten ist ein Screenshot des Popup-Fensters und des Accessibility Inspector-Fensters.
BEARBEITEN: Hier ist eine abgespeckte Version dessen, was ich versuche, aber auf einer öffentlichen Website. Es scheint, als ob die Javascript-Warnung verhindert, dass irgendetwas im Browser passiert, bis manuell auf OK geklickt wird.
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
Sie können eine Funktion verwenden, um window.alert
jede Warnung abzufangen und diese Warnung abzubrechen , verwenden Sie eine Variable, um zu sehen, ob es einen Warnungsversuch gab.
So was :
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
Sie können entweder den AppleScript Accessibility Inspector oder eine Variante dieses Skripts verwenden, um den Namen des Dialogfensters oder den darin enthaltenen Text oder ein anderes identifizierendes Merkmal abzurufen. Wenn Ihr Skript diese Informationen findet, bedeutet dies, dass das Dialogfeld eingeblendet wurde und Sie Maßnahmen ergreifen können.
-- Entire Contents Demo - mini
-- BP ages ago or so
-- This'll get all the controls and structures associated with an App's window and menus
-- In a form which is easily pasteable into your own scripts
-- and show them in the result pane below.
--
-- Copy that into a text editor and change commas to returns to get an easily readable list.
--
-- The script can take a long time if there are LOTS of window items, such as
-- in the "music" pane of iTunes. It may even time out if you have a huge iTunes library
-- The script'll process most App's UI structures in under a minute
set appname to "System Preferences" -------------------------- Set this to the App you want to look at
set winstuff to "defaultval"
set menustuff to "defaultval"
tell application appname
activate
end tell
tell application "System Events"
tell process appname
set winstuff to entire contents of front window
set menustuff to entire contents of menu bar 1
end tell
end tell
--return winstuff & "rrrr" & menustuff -- comment this out to get just winstuff
return winstuff -- comment this out too to get just menustuff
--return menustuff
Brody Maler
Wandernder Fremder
Brody Maler
Wandernder Fremder
Brody Maler