问题
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