来自getInfoByClass的简单错误?

时间:2016-07-14 21:10:46

标签: javascript debugging applescript

我正在尝试使用AppleScript从网站上获取一些信息,并且我不断获得"预期的行尾等,但找到了标识符。"突出显示JavaScript的错误

下面是错误代码的部分

to getInputByClass(theClass, num)

    tell application "Google Chrome" 

        set input to do JavaScript "document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;" in document 1 

    end tell

    return input

end getInputByClass

我也尝试过使用

tell application "Google Chrome"
    activate
    set theScript to "document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;"
    do JavaScript theScript in current tab of first window
end tell

感谢您的帮助!!!

2 个答案:

答案 0 :(得分:1)

我的整个代码(非常简短)现在如下所示。我无法获取代码将从javascript获取的信息发布到b列。你会怎么做才能解决这个问题。再次感谢你。

将i设为1

重复725次

activate application "Microsoft Excel"

tell application "Microsoft Excel"
    select cell ("a" & (i as string))
end tell

tell application "System Events"
    keystroke "c" using command down
    delay 1
end tell

activate application "Google Chrome"

tell application "System Events"
    keystroke "t" using command down
    delay 1
    keystroke "https://bookscouter.com/prices.php?isbn="
    keystroke "v" using command down
    keystroke "&searchbutton=Sell"
    delay 1
    keystroke return
    delay 10
end tell

set theText to getInputByClass("book-prices", 0)
tell application "Microsoft Excel"
    set value of cell ("b" & (i as string)) to theText
end tell

set i to i + 1

tell application "System Events"
    keystroke "w" using command down
end tell

结束重复

到getInputByClass(theClass,num)

tell application "Google Chrome"
    tell active tab of window 1
        set input to execute javascript "document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;"
    end tell
end tell

return input

结束getInputByClass

答案 1 :(得分:0)

查看脚本字典。没有为 Google Chrome 定义do javascript - 处理程序。您会找到execute

to getInputByClass(theClass, num)

    tell application "Google Chrome"
        tell active tab of window 1
            set input to execute javascript "document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;"
        end tell
    end tell

    return input

end getInputByClass

如果您使用

调用此处理程序
getInputByClass("current-site-link site-link js-gps-track", 0)

你应该得到

<div class=\"site-icon favicon favicon-stackoverflow\" title=\"Stack Overflow\"></div>
            Stack Overflow

结果。

玩得开心,迈克尔/汉堡