2017-09-14 60 views
0

我有一個腳本,可以在新的Safari窗口中打開URL列表,但JavaScript命令似乎不起作用。我卡在一個console.log文件中,該文件不會在檢查器中顯示,也不會顯示出來。我試着改變了JavaScript之前的延遲,一直到17。JavaScriptScript命令在AppleScript中工作

作爲一個備註,'subscribe-button'是編輯按鈕,當你登錄,我想點擊。

on run {input, parameters} 
    read (item 1 of input) 
    set ps to paragraphs of the result 
    set tot to count ps 
    tell application "Safari" 
     reopen 
     activate 
    end tell 
    repeat with i from 1 to tot 
     set p to item i of ps 
     if p is not "" then 
      try 
       tell application "Safari" 
        tell front window 
         set r to make new tab with properties {URL:p} 
         set current tab to r 
         delay 1 
         do JavaScript "console.log('test this log')" 
         do JavaScript "document.getElementsById('subscribe-button').click()" 
         if i = tot then exit repeat 
         repeat 
          delay 1 
          get URL of r 
         end repeat 
        end tell 
       end tell 
      end try 
     end if 
    end repeat 
end run 
+0

如果您需要幫助調試代碼,您應該提供[最小,完整和可驗證示例](https://stackoverflow.com/help/mcve)。 – user3439894

+0

這真的是無益的評論,但我仍然回答自己的問題。 – VagueExplanation

+0

我沒有任何問題在我已經應用到的網頁中,使用JavaScript中的'do JavaScript'document.getElementsById('elementID')。click()「在文檔1中單擊使用JavaScript中的JavaScript按鈕。至於我對** MCVE **的評論,這幾乎是** SO **的潛規則,但有時提醒是必要的! **您沒有提供URL來測試您的代碼,或查看目標頁面中的代碼,所以我所能提供的是反覆爲我和其他人工作的內容。** – user3439894

回答

0

我發現,如果我都將JavsScript設置爲「在當前選項卡」並增加延遲,console.log的作品。

on run {input, parameters} 
    read (item 1 of input) 
    set ps to paragraphs of the result 
    set tot to count ps 
    tell application "Safari" 
     reopen 
     activate 
    end tell 
    repeat with i from 1 to tot 
     set p to item i of ps 
     if p is not "" then 
      try 
       tell application "Safari" 
        tell front window 
         set r to make new tab with properties {URL:p} 
         set current tab to r 
         delay 10 
         do JavaScript "console.log('test this log')" in current tab 
         if i = tot then exit repeat 
         repeat 
          delay 10 
          get URL of r 
         end repeat 
        end tell 
       end tell 
      end try 
     end if 
    end repeat 
end run 
+0

是否有任何理由讓您遺漏了'在你的答案中做「JavaScript」document.getElementsById('subscribe-button')。click()「?它點擊了按鈕? – user3439894

+0

不,主要是我的問題是讓JavaScript運行。它給了我一個錯誤,那個想法的元素不存在,我認爲這可能是由於頁面沒有完全加載而引起的。這可能會延遲修復,或另一個腳本檢測頁面是否加載。我不再點擊該按鈕,因爲我只能使用帶有相關YouTube視頻ID的通用視頻編輯網址。 – VagueExplanation