我已经成功地编写了一个非常有用的脚本:)
它以Chrome隐身模式打开正确的网页,在加载时等待12秒,导航到页面的正确部分并打开投票面板。到目前为止一切都很好。
我似乎无法使用JavaScript来点击投票按钮,尽管尝试了许多变体,例如名称等。
代码有明显错误吗?
运行{input,parameters}
do shell script "open -a /Applications/Google\\ Chrome.app --args --incognito"
activate application "Google Chrome"
open location "http://www.schoolgen.co.nz/voteforyourschool/"
delay 12.0
tell application "System Events"
keystroke tab
keystroke tab
keystroke tab
keystroke tab
keystroke tab
keystroke tab
keystroke tab
keystroke tab
keystroke tab
keystroke tab
keystroke tab
keystroke "ahuroa"
keystroke tab
keystroke return
delay 0.5
end tell
tell application "Google Chrome"
execute javascript "document.getElementsById( 'votebutton' ).click();"
delay 0.5
end tell
--tell application "Google Chrome" to quit
结束
答案 0 :(得分:1)
以下是我对您尝试执行的操作进行编码的方法,并且我已经测试了以下示例 AppleScript 代码,它可以实现目标。< / p>
if running of application "Google Chrome" then tell application "Google Chrome" to quit
delay 1
do shell script "open -a 'Google Chrome' --args --incognito http://www.schoolgen.co.nz/voteforyourschool/"
delay 1
tell application "Google Chrome"
activate
-- # Wait until page finishes loading.
repeat until (loading of active tab of front window is false)
delay 1
end repeat
tell active tab of front window
-- # Click the "Vote now" button for "Ahuroa School".
execute javascript "document.getElementsByClassName('flotediv col-md-3 col-xs-12 col-sm-6 flexbutton ')[77].click();"
delay 0.5
-- # Click the "Vote" button.
execute javascript "document.getElementsByClassName('votebutton')[0].click();"
delay 0.5
-- # Click the "Close" button.
execute javascript "document.getElementsByClassName('btn btn-default')[0].click();"
delay 0.5
end tell
quit
end tell
我编写了它,所以如果谷歌Chrome已经打开,它首先关闭,以便可以在隐身模式下重新打开,然后等待页面加载完毕,同时让JavaScript处理实现目标所需的其余部分,所以UI Scripting例如<系统事件“和keystroke
命令不需要使用。
顺便说一句,虽然你的大多数代码都有效,但有两个失败点,一个是execute javascript
未被告知在哪里这样做,因此在tell active tab of front window
<中添加了tell application "Google Chrome"
em>阻止,而不是使用getElementsById
,而tell active tab of front window
即使用getElementsByClassName
也不行,我使用了"document.getElementsByClassName('flotediv col-md-3 col-xs-12 col-sm-6 flex button ')[77]"
,因为它有效。
我再次更新了我的答案。按目前编码,它不需要搜索“ahuroa”,而只需点击“Ahuroa School”的“现在投票”按钮,即"document.getElementsByClassName('flotediv col-md-3 col-xs-12 col-sm-6 flex button ')[?]"
,尽管这可能会改变。如果是,您可以增加或减少数字,或者您可以使用以下版本的代码来确定与if running of application "Google Chrome" then tell application "Google Chrome" to quit
delay 1
do shell script "open -a 'Google Chrome' --args --incognito http://www.schoolgen.co.nz/voteforyourschool/"
delay 1
tell application "Google Chrome"
activate
-- # Wait until page finishes loading.
repeat until (loading of active tab of front window is false)
delay 1
end repeat
tell active tab of front window
-- # Click the "Vote now" button for "Ahuroa School".
set theCount to execute javascript "document.getElementsByClassName('schoolnameid').length;"
repeat with i from 0 to theCount
set theSchoolName to execute javascript "document.getElementsByClassName('schoolnameid')[" & i & "].innerText;"
if theSchoolName is "Ahuroa School" then
execute javascript "document.getElementsByClassName('flotediv col-md-3 col-xs-12 col-sm-6 flexbutton ')[" & i & "].click();"
exit repeat
end if
end repeat
delay 0.5
-- # Click the "Vote" button.
execute javascript "document.getElementsByClassName('votebutton')[0].click();"
delay 0.5
-- # Click the "Close" button.
execute javascript "document.getElementsByClassName('btn btn-default')[0].click();"
delay 0.5
end tell
quit
end tell
一起使用的适当数字。
Codable
注意: 示例 AppleScript 代码就是这样,并且不使用任何错误处理并且仅用于显示完成任务的多种方式之一。用户有责任根据需要添加/使用适当的错误处理。