Applescript点击搜索结果/其他网页按钮和链接?

时间:2014-02-24 20:50:35

标签: applescript

如何使用Applescript在Google搜索中点击网络链接。我可以通过姓名,号码或其他任何方式识别它们吗?

2 个答案:

答案 0 :(得分:2)

Safari浏览器:

tell application "Safari"
    open location "http://google.com/search?q=example"
    do JavaScript "window.addEventListener('load', function() {
      document.querySelectorAll('.r a')[0].click()
    })" in document 1
end tell

铬:

tell application "Google Chrome"
    open location "http://google.com/search?q=example"
    tell active tab of window 1
        execute javascript "window.addEventListener('load', function() {
          document.querySelectorAll('.r a')[0].click()
        })"
    end tell
end tell

修改:尝试使用以下内容匹配YouTube结果标题:

document.querySelector('.yt-uix-tile-link').click()

修改2:将window.onload=function(){}更改为window.addEventListener('load',function(){})

答案 1 :(得分:1)

基于@Lauri Ranta的优秀答案,这里是便利功能clickOn() ,其中:

  • 接受目标文档,CSS选择器字符串加上从零开始的索引,以选择选择器字符串匹配的内容,并模拟由此标识的元素上的单击
  • 无论目标文档是否仍在加载或已经完全加载工作 - 结果证明这是非常重要的。 (Lauri的代码依赖于window对象的load事件,但尚未解雇。)
  • 适用于Safari和谷歌浏览器(如果您没有安装Chrome,则必须注释掉几行)

示例使用延迟来证明即使在文档完全加载后点击也能正常工作:

# SAFARI
# Click on the *2nd* result returned from googling 'example' with Safari:
tell application "Safari"
  open location "http://google.com/search?q=example"
  delay 5 # sample delay - NOT needed
  my clickOn(document 1, ".r a", 1)
end tell

# CHROME
tell application "Google Chrome"
  open location "http://google.com/search?q=example"
  delay 5 # sample delay - NOT needed
  my clickOn(active tab of window 1, ".r a", 1)
end tell

clickOn源代码:

on clickOn(doc, cssSelector, ndx)

    # If no explicit index (into the matches returned by the CSS selector)
    # is specified, default to 0.
    if ndx is missing value or ndx = "" then set ndx to 0

    # Synthesize the JavaScript command.
    set jsCode to "
     (function () { // Use a closure to avoid polluting the global namespace.
         function doIt(t) { // Helper function
           if (doIt.done) return; // Already successfully called? Ignore.
           try {
                // Perform the desired action - may fail if invoked too early.                              
                document.querySelectorAll('" & cssSelector & "')[" & ndx & "].click();
           } catch(e) { 
                return; // Return without setting the success 'flag'.
           }
           doIt.done=true; // Set success 'flag' as a property on this function object.
      };
      // Attach a listener to the window's load event for invoking the helper function then.
      window.addEventListener('load', doIt);
      // If the document signals readiness -- which may still be too early, we can't know --
      // also try to invoke the helper function *directly*.
      if (document.readyState === 'complete') { doIt(); }
    })();
"
    # Execute the JavaScript command in the target page.
    if class of doc as text is "document" then # Safari: a «document» instance was passed
        using terms from application "Safari"
            tell doc to do JavaScript jsCode
        end using terms from
    else # Google Chrome: a «tab» instance was passed
    # !! IF CHROME IS NOT INSTALLED, SIMPLY DEACTIVATE THIS `using terms from` BLOCK.
        using terms from application "Google Chrome"
            tell doc to execute javascript jsCode
        end using terms from
    end if

end clickOn