【Apple Script】自動でSafariを開いてボタンをクリックするスクリプト

Apple Script + Safari でプログラム開発する時は、Javascript も使えるように、

support.apple.com

Safari」>「環境設定」と選択して、「詳細」をクリックして、「メニューバーに“開発”メニューを表示」を選択します。

Apple Event からJavascript を許可」にチェックを入れておく。

Script は、↓こちらのサイトを参考にして書きました。

mac-tegaki.com

あるサイトを開いてボタンをクリックする例:

tell application "Safari"
activate
tell window 1
open location "【開きたいサイトのURL】"
my isLoaded(0.5, 20, "【開きたいサイトのURL】")
my clickID("【クリックしたいボタンの id】")
end tell
end tell
-- ページを読み込むまで待つ
on isLoaded(delayTime, repeatCount, domainText)
tell application "Safari"
repeat repeatCount times
if (URL of document 1 as text) contains domainText then
log (URL of document 1 as text)
repeat repeatCount times
if (do JavaScript "document.readyState" in document 1) is "complete" then
return true
end if
delay delayTime
end repeat
return false
end if
delay delayTime
end repeat
return false
end tell
end isLoaded
-- 指定したIDのボタンの存在を確認したらクリックする
to clickID(theId)
tell application "Safari"
do JavaScript "document.getElementById('" & theId & "').click();" in document 1
end tell
end clickID

自動ログインをしたいと思っても、

2重ログインなどが増えてきそうなので、Selenium よりも、Apple Script + Safari の方が有効かもしれない。

Mac さえあれば、新たに環境を作らなくてもいいし。

mc909j.blogspot.com

返信を残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA