Estoy usando AppleScript con Google Chrome para automatizar un proceso tedioso en un sitio web. En ciertas condiciones, el sitio web tiene una ventana emergente de alerta para notificar al usuario sobre un problema. Esto evita que se complete el resto de la secuencia de comandos del automatizador. Necesito detectar esta ventana emergente, registrarla en un archivo (ya lo tengo resuelto) y hacer clic en el botón Aceptar para continuar. El código que tengo actualmente se ve así:
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
Hasta ahora, lo que tengo funciona bien cuando no hay un mensaje emergente, sin embargo, requiere una intervención manual cuando se activa una ventana emergente. A continuación se muestra una captura de pantalla de la ventana emergente y la ventana del Inspector de accesibilidad.
EDITAR: Aquí hay una versión simplificada de lo que estoy tratando de hacer, pero en un sitio público. Parece que la alerta de javascript impide que suceda algo en el navegador hasta que se haga clic manualmente en Aceptar.
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
Puede usar una función para window.alert
interceptar cualquier alerta y para cancelar esta alerta , use una variable para ver si hubo un intento de alerta.
Como esto :
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
Puede utilizar el Inspector de accesibilidad de AppleScript o alguna variante de este script para obtener el nombre de la ventana de diálogo, el texto que contiene o alguna otra característica de identificación. Si su secuencia de comandos encuentra esa información, significa que ha aparecido el cuadro de diálogo y puede tomar medidas.
-- 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
pintor brody
Extraño caminante
pintor brody
Extraño caminante
pintor brody