首页 > 解决方案 > 无法获得关于何时使用 applescript 调用 jquery 的正确时机

问题描述

我正在尝试使用 applescript 和 javascript (jquery) 的组合来模拟作为 microsoftoline.com 中的特定用户的登录。首先,我需要知道“以前登录到 MSFT 列表”中有多少用户。我尝试通过检查 MSFT 登录屏幕中列出的行数来做到这一点。一旦我有了“Numusers”,然后我在每一行中迭代 HTML 以单击感兴趣的用户 ID div。问题是,我似乎无法确定何时运行 jquery 以获得这么多用户的时间。我有所有其他部分工作,只是不是 jquery 部分。

到目前为止,这是我的代码:

tell application "Safari"   
    activate
    make new document with properties {URL:"http://login.microsoftonline.com"}
    delay 3
set NumUsers to do JavaScript "$('div.tile-container').length" in 
current tab of first window
display dialog NumUsers

end tell

作为回应,我收到一条错误消息,指出“未定义变量 NumUsers”。

我已经尝试过很多事情,包括在调用 jquery 之前等待页面加载。但我似乎也无法让它工作。该特定的编码尝试如下所示:

tell application "Safari"
    activate
   if not (exists document 1) then reopen
    tell current tab of window 1 to set URL to "http://login.microsoftonline.com"

set the_state to missing value
repeat until the_state is "complete"
    set the_state to (do JavaScript "document.readyState" in document 1)
    log "busy"
    delay 0.2
end repeat
log "complete"
set NumUsers to do JavaScript "$('div.tile-container').length" in current tab of window 1
delay 2
display dialog NumUsers
end tell

我不喜欢使用 safari 作为源浏览器。

标签: javascriptjqueryapplescript

解决方案


毕竟不需要使用jquery。

这有效:

    set NumUsers to do JavaScript "document.getElementsByClassName('tile-container').length" in current tab of window 1

推荐阅读