【AppleScript】Javascriptの値を取得する方法

AppleScriptSafariブラウザを操作する時、

アンカーのテキストからエレメントを取得できないか調べてみた。

on run
tell application "Safari"
activate
tell window 1
log "start!"
set url1 to "https://www.google.com"
open location url1
my isLoaded(0.5, 20, url1)
set test1 to my isAnchorText("ログイン")
if test1 = "" then
log "error01"
return
end if
log test1
--close
end tell
end tell
end run
to isAnchorText(theText)
tell application "Safari"
set val to do JavaScript "
      val = '';
      var lists = document.getElementsByTagName('a');
      for ( var i = 0; i < lists.length; i++ ) {
          var item = lists[i];
          if(item.innerHTML == '" & theText & "'){
              val = item.innerHTML;
              break;
          }
      }
      res = val;
      " in document 1
return val
end tell
end isAnchorText
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

do JavaScript は、「最後のグローバル値」を返すので、そこだけ注意!

www.dev-dev.net

返信を残す

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

CAPTCHA