Why doesn't javascript call in Applescript if the same code entered into the browser does?

I am trying to understand why my Applescript does nothing when the same javascript code entered into the Safari location bar is running.

Go to the search results page, for example: http://www.google.com/search?q=test . For proper behavior, enter it in the location bar and press enter:

javascript: document.getElementsByClassName('vspib')[0].click(); 

You will see that he selects a magnifying glass for the first search result.

This is what I want to do through javascript. So I typed the following:

 tell application "Safari" activate do JavaScript "document.getElementsByClassName('vspib')[0].click();" in document 1 end tell 

However, he does nothing. Any ideas?

+6
source share
1 answer

The problem is that do JavaScript should correctly address the tab in the Safari window.

A script works for me if the search results page is the current tab in the very front window of Safari:

 tell application "Safari" activate set theScript to "document.getElementsByClassName('vspib')[0].click();" do JavaScript theScript in current tab of first window end tell 
+14
source

Source: https://habr.com/ru/post/891899/


All Articles