如何使用AppleScript搜索Safari的“智能”下拉菜单

问题描述 投票:0回答:1

我有一个代码,可以打开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

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

safari applescript automator
1个回答
0
投票

我在您的代码中做了一些小的编辑。现在,此版本的代码我对其进行了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版本是“版本10.1.2(12603.3.8)” @ MacOS 10.12.6。让我知道是否有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.