Как использовать AppleScript для поиска в «умном» выпадающем меню Safari - PullRequest
0 голосов
/ 22 марта 2020

У меня есть код, который открывает Safari для bing, вставляет что-то для поиска и выполняет поиск. Тем не менее, я пытаюсь заставить скрипт вводить поиск THEN go вниз и искать результаты автозаполнения, а не фактическое поисковое слово. До сих пор я не смог этого сделать. Вот разбивка:

 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

В конечном счете, целью является случайный поиск результатов автозаполнения на основе введенных данных. Код до сих пор не работает и будет просто искать исходный текст ввода.

1 Ответ

1 голос
/ 24 марта 2020

Я сделал несколько небольших правок в вашем коде. Сейчас эту версию кода я тестирую более 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. Дайте мне знать, если это поможет.

...