AppleScript通过JavaScript

时间:2017-05-09 19:23:51

标签: javascript forms safari applescript registration

我试图通过Applescript强制注册。有一个表单字段需要电子邮件。我已成功将电子邮件添加到表单字段,但仍未将其识别为“值”。当我检查元素时,没有设置值。但是,如果我单击表单域并添加一个空格,则会识别该值。我不知道如何解决这个问题。我试图使用focus(),click(),多次击键,无论我做什么,都不会使用Applescript设置值。

set email to the text returned of (display dialog "Enter in Your Email to Validate Your Purchase:" default answer "")

tell application "Safari"
        make new document at end of documents
        set URL of document 1 to "https://myurl.com"
        tell application "System Events" 

            set theScript to "document.getElementById('email').value= '" & email & "';"

        end tell

        delay 3 

        do JavaScript theScript in document 1

end tell

2 个答案:

答案 0 :(得分:1)

我找到了答案:

tell application "Safari"
make new document at end of documents
set URL of document 1 to "https://www.myurl.com"

        delay 1
        do JavaScript "document.getElementById('email').focus();" in document 1
        delay 1
        do JavaScript "document.getElementById('email').select();" in document 1
        delay 1
        do JavaScript "document.getElementById('email').value = '" & email & "';" in document 1
        delay 1
        tell application "System Events"
            keystroke space
        end tell
end tell

答案 1 :(得分:0)

感谢发帖,尼克!

我需要添加退货。以下代码适用于已打开网页的选项卡,因此无需设置 url。

to inputByID(theId, theValue) -- creates the function

tell application "Safari" -- tells AppleScript to use Safari
    
    do JavaScript "  document.getElementById('" & theId & "').Focus();" in document 1
    
    delay 1
    
    do JavaScript "  document.getElementById('" & theId & "').Select();" in document 1
    
    delay 1
    
    do JavaScript "  document.getElementById('" & theId & "').value ='" & theValue & "';" in document 1
    
    delay 1
    
    tell application "System Events"
        keystroke space
        keystroke return
    end tell
    
end tell -- stops using safari

end inputByID --标记函数结束