2013-10-23 42 views
1

我想使用AppleScript通過Safari自動登錄網頁。使用AppleScript自動點擊對話框按鈕

當我打開一個URL並給出用戶名和密碼時,如果它是第一次還沒有保存密碼,它會顯示消息:「你想保存這個密碼嗎?」。

我想使用AppleScript來選擇「不是現在」。我怎樣才能做到這一點?

這裏是我當前的代碼:

tell application "Safari"      
     open location "http://www.amazon.com"    
     activate     
end tell       

if page_loaded(60) then      
     say "Page Loaded"    

     tell application "Safari"    
       set loginURL to do JavaScript "document.getElementById('nav-your-account').getAttribute('href')" in document 1  
       open location loginURL  
     end tell     

else       
     say "Page Failed to load or Safari didn't open"    
end if      

if page_loaded(60) then      
     say "Page Loaded"    

     tell application "Safari"    
       do JavaScript "document.getElementById('ap_email').value = 'mritjp'; document.getElementById('ap_password').value = 'mritjp'; document.forms['signIn'].submit();" in document 1  
       ---HOW TO CONTROL TO CHOOSE "Not Now" in message "Would you like to save this password?" of safari 
       activate   
       delay 2  
     end tell     
else       
     say "Page Failed to load or Safari didn't open"    
end if      



on page_loaded(timeout_value) -- in seconds      
     delay 1    
     repeat with i from 1 to timeout_value    
       tell application "Safari"  
         if name of current tab of window 1 is not "Loading" then exit repeat 
       end tell   
       delay 1  
     end repeat    
     if i is timeout_value then return false    
     tell application "Safari"    
       repeat until (do JavaScript "document.readyState" in document 1) is "complete"  
         delay 0.5 
       end repeat  
     end tell     
     return true    
end page_loaded 

Fixed by comment of Lauri Ranta

如果輔助設備的控制已經從系統首選項啓用後,您可以使用系統事件:

on enabledGUIScripting() 
    do shell script "sudo touch /private/var/db/.AccessibilityAPIEnabled" password "1" with  administrator privileges 
end enabledGUIScripting() 

Then:

tell application "System Events" to tell process "Safari" 
    click button 3 of sheet 1 of window 1 
end tell 
+0

如果勞裏·蘭塔的回答是解決你的問題,請點擊綠色的對勾答案旁邊接受。 –

回答

2

如果輔助設備的控制已經從系統首選項啓用後,您可以使用系統事件:

tell application "Safari" 
    do JavaScript "document.getElementById('login_button_id').click()" in document 1 
end tell 
delay 1 
tell application "System Events" to tell process "Safari" 
    click button 3 of sheet 1 of window 1 
end tell 
+0

謝謝你的幫助。作爲您的答案:[輔助設備的訪問權限已從系統偏好設置中啓用]。 我也寫一個腳本來自動啓用,但我們不能處理輸入的用戶名,密碼或自動點擊解鎖「SecurityAgent」(因爲輔助設備的控制方面一直沒有啓用) –

+0

完成: 上enabledGUIScripting() \t使用管理員權限執行shell腳本「sudo touch /private/var/db/.AccessibilityAPIEnabled」密碼「1」 end enabledGUIScripting –

+0

這是否適用於safari之外的其他功能?我在illustrator中有一個對話框的問題。 –

相關問題