Applescript use google chrome to extract text

倖福魔咒の 提交于 2019-12-11 04:27:53

问题


I'm extracting text from a page on safari, everything work fine, However I can't adapt this script to Google Chrome

Here is the Safari code

to getInputByClass2(theClass, num) 
    tell application "Safari" 
        set input to do JavaScript "
document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;" in document 1 
    end tell
    return input 
end getInputByClass2

Here is the Chrome code

 to getInputByClass2(theClass, num) 
    tell application "Google Chrome" 
        set input to do JavaScript "
document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;" in document 1 
    end tell
    return input 
end getInputByClass2

have error "Expected end of line but found identifier."


回答1:


The command and the syntax for "Google Chrome" is not the same.

You must use the execute javascript command instead of do javascript.

And you must specify a tab from a window instead of in document 1.

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


来源:https://stackoverflow.com/questions/37299708/applescript-use-google-chrome-to-extract-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!