使用 Applescript 在 Safari 中按名称单击按钮

Button Click by Name in Safari with Applescript

我正在学习教程:http://cubemg.com/applescript/how-to-click-a-button-on-a-web-page-with-applescript/

我正在尝试制作一个脚本来加载网页并按名称单击按钮。 我的代码看起来像这样

tell application "Safari"
    activate
    do JavaScript "window.open('http://www.google.dk')" in document 1   
end tell
to clickName(theName, elementnum)
    tell application "Safari"
        do JavaScript "document.getElementsByName(‘" & theName & "‘)[" & elementnum & "].click();" in document 1
    end tell
end clickName
clickName("btnI", 0)

它只是在 safari 中打开页面,什么都不做。 Applescript 一直告诉我 "missing value" 但即使我 运行 代码仅用于打开特定网站,它也会这样做。 我已经创建了一个类似的代码来将值输入到表单中,但结果是一样的。

有没有人遇到同样的问题?

我已经多次检查按钮的名称,并在多个不同的网站上尝试过。

您的脚本中有 typo/bug。此行使用 "smart quotes" 作为单引号。这个:

do JavaScript "document.getElementsByName(‘" & theName & "‘)[" & elementnum & "].click();" in document 1

应该是这个

do JavaScript "document.getElementsByName('" & theName & "')[" & elementnum & "].click();" in document 1

此外,您确实需要在加载网页后延迟,以确保加载元素。