最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

macos - why doesn't 'do javascript' call in Applescript execute when the same code typed into browser do

programmeradmin10浏览0评论

I'm trying to figure out why my Applescript does nothing when the same javascript code typed into a Safari location bar works.

Go to a search results page, such as: . For the correct behavior, type this into the location bar and hit enter:

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

You'll see that it selects the magnifier for the first search result.

This is what I want to make happen via javascript. So I typed up the following:

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

However, it does nothing. Any ideas?

I'm trying to figure out why my Applescript does nothing when the same javascript code typed into a Safari location bar works.

Go to a search results page, such as: http://www.google.com/search?q=test . For the correct behavior, type this into the location bar and hit enter:

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

You'll see that it selects the magnifier for the first search result.

This is what I want to make happen via javascript. So I typed up the following:

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

However, it does nothing. Any ideas?

Share Improve this question asked Jul 3, 2011 at 6:29 mixmix 7,15115 gold badges65 silver badges92 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 16

The problem is that do JavaScript has to correctly address a tab in a Safari window.

The following script works for me, if the search results page is the current tab in the frontmost Safari window:

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论