如何使用AppleScript搜索Safari';s";“聪明的”;下拉菜单

如何使用AppleScript搜索Safari';s";“聪明的”;下拉菜单,safari,applescript,automator,Safari,Applescript,Automator,我有一个代码,可以打开Safari到bing,插入要搜索的内容,然后进行搜索。但是,我试图让脚本输入搜索,然后向下搜索自动填充结果,而不是实际的搜索词。到目前为止,我一直无法做到这一点。以下是分项数字: set theURL to "https://www.bing.com" tell application "Safari" to make new document with properties {URL:theURL} tell application "System Events

我有一个代码,可以打开Safari到bing,插入要搜索的内容,然后进行搜索。但是,我试图让脚本输入搜索,然后向下搜索自动填充结果,而不是实际的搜索词。到目前为止,我一直无法做到这一点。以下是分项数字:

 set theURL to "https://www.bing.com"

 tell application "Safari" to make new document with properties {URL:theURL}
 tell application "System Events"
      repeat until exists (UI elements of groups of toolbar 1 of window 1 of application process "Safari" whose name = "Reload this page")
           delay 0.5
      end repeat
 end tell

 inputByID("sb_form_q", "coronavirus")
 delay 0.5
 clickID("sb_form_q")  -- Here is where it breaks down. Trying to "click" the search bar after pasting so that System Events can 'arrow down'to the autofill results.
 tell application "System Events"
      key code 125
      key code 125
 end tell

 delay (random number from 5 to 15)
 clickID("sb_form_go")
 delay 0.5
 tell application "System Events"
      delay 1.5
      keystroke "w" using command down
 end tell

 to inputByID(theID, theValue)
      tell application "Safari"
           do JavaScript "  document.getElementById('" & theID & "').value ='" & theValue & "';" in document 1
      end tell
 end inputByID

 to clickID(theID)
      tell application "Safari"
           do JavaScript "document.getElementById('" & theID & "').click();" in document 1
      end tell
 end clickID

最终,目标是根据输入随机搜索自动填充结果。到目前为止,代码不起作用,只会搜索原始输入文本

我对你的代码做了一些小的编辑。现在这个版本的代码,我测试了40多次。没有一个输。我在代码中写了很多注释。你可以在你的机器上测试它

set theURL to "https://www.bing.com"

tell application "Safari"
    activate
    delay 0.3
    -- 0th edit:    I find "activate" is nessary for testing the code
    make new document with properties {URL:theURL}
end tell

tell application "System Events"
    tell process "Safari"
        --1st edit: here "tell process "Safari"" is a must on my machine. 
        repeat until exists (button "Reload this page" of UI element 1 of group 2 of toolbar 1 of window 1)
            delay 0.5
            --2nd edit: the original line in your post "repeat until exists (UI elements of groups of toolbar 1 of window 1 of application process "Safari" whose name = "Reload this page")" doesn't work on my machine. If this doesn't work on your machine, you can change it back to your line.
        end repeat
    end tell
end tell

inputByID("sb_form_q", "coronavirus")
delay 0.3

--clickID("sb_form_q")
--delay 0.3
--3rd edit: I find this line is not nessary on my machine. Do you mean you use "arrow down" to show the autofill menu in your post? On my machine, after input "coronavirus", the dropdown autofill menu appears immediately and automatically. No need to use "arrow down" to show the dropdown menu.

tell application "System Events"
    set randomNumber to random number from 1 to 8
    repeat randomNumber times
        key code 125
        delay 0.3
    end repeat
end tell

--delay (random number from 5 to 15)
clickID("sb_form_go")
--delay 0.5

--4th edit: actually i am not sure I understand what does the word "Randomly" mean in your post. I change the code here to select option in autofill randomly then execute the search.

tell application "System Events"
    delay 2
    keystroke "w" using command down
end tell

to inputByID(theID, theValue)
    tell application "Safari"
        do JavaScript "  document.getElementById('" & theID & "').value ='" & theValue & "';" in document 1
    end tell
end inputByID

to clickID(theID)
    tell application "Safari"
        do JavaScript "document.getElementById('" & theID & "').click();" in document 1
    end tell
end clickID

我的Safari版本是“Version10.1.2(12603.3.8)”@MacOS 10.12.6。如果有帮助,请告诉我。

我想是因为增加了告诉Safari“激活”的功能,键代码“向下箭头”才起作用。非常感谢。