我是AppleScript
的新手,我想弄清楚如何激活选定的元素。
我想让野生动物园激活目标元素的可悲尝试:
我通过选择来选择元素。活动元素是播放按钮:
。
但是,当我尝试运行脚本时,出现以下错误:
。
有人可以告诉我我的错误和/或显示解决方法的示例吗?
答案 0 :(得分:0)
使用AppleScript代码中的JavaScript,您可以在Script Editor应用中使用以下代码在Safari中单击YouTube视频上的播放/暂停按钮
clickYouTubePlayButton("ytp-play-button ytp-button", 0)
to clickYouTubePlayButton(theClassName, elementnum)
if application "Safari" is running then
try
tell application "Safari"
tell window 1 to set current tab to tab 1 whose URL contains "youtube"
do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
end tell
end try
end if
end clickYouTubePlayButton
响应用户@ user3439894的评论,关于必须启用Safari才能允许JavaScript事件,以上代码才能正常工作...以下代码将自动在需要时在Safari中启用JavaScript事件的过程是..
使用最新版本的macOS Mojave和最新版本的Safari进行测试
global windowName, errMsg, errNum
clickYouTubePlayButton("ytp-play-button ytp-button", 0)
to clickYouTubePlayButton(theClassName, elementnum)
if application "Safari" is running then
try
tell application "Safari"
tell window 1 to set current tab to tab 1 whose URL contains "youtube"
do JavaScript "document.getElementsByClassName('" & theClassName & "')
[" & elementnum & "].click();" in document 1
end tell
on error errMsg number errNum
if errNum is 8 then
my enableJavascript()
end if
end try
end if
end clickYouTubePlayButton
on enableJavascript()
tell application "Safari" to activate
tell application "System Events"
tell application process "Safari"
click menu bar item "Safari" of menu bar 1
click menu item 4 of menu 1 of menu bar item "Safari" of menu bar 1
delay 1
set windowName to get name of window 1
click UI element "Advanced" of toolbar 1 of window windowName
click checkbox "Show Develop menu in menu bar" of group 1 of group 1 ¬
of window "Advanced"
click UI element 2 of window "Advanced"
click menu bar item "Develop" of menu bar 1
click menu item "Allow JavaScript from Apple Events" of menu 1 of ¬
menu bar item "Develop" of menu bar 1
delay 0.5
if exists of UI element "Allow" of window 1 then
click UI element "Allow" of window 1
end if
end tell
end tell
end enableJavascript