如何使用 AppleScript 搜索 Safari 的 "smart" 下拉菜单
How to use AppleScript to search Safari's "smart" dropdown menu
我有一个代码可以打开 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 版本是 "Version 10.1.2 (12603.3.8)"@MacOS 10.12.6。如果有帮助,请告诉我。
我有一个代码可以打开 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 版本是 "Version 10.1.2 (12603.3.8)"@MacOS 10.12.6。如果有帮助,请告诉我。