问题
Here is my situation in detail.
I have figured out how to login to a website, not using javascript, but using "keystrokes", "tabs" and "returns" to find the username and password fields. I know this can be unreliable as websites change but for now I am just trying to keep it simple.
After I log in to a site, like a bank account, I want to grab the account balance and import it to Numbers.
I have figured out how to insert a predermined text into a specific cell in Numbers and probably could expand that to dialog boxes ect, but that is not what I am really trying to accomplish.
Basically I don't know what I need to do to grab the data from the current website after I have logged in. Doing research I realize it is something todo with javascript.
For now and to apply it to the bigger plan, I just want to know how to locate data of a website that I do not have to log into in Safari via javascript and display the data in the results of Applescript Editor. And I am trying to figure out how I could use that result to display it in another application (ei Numbers). I then would apply this to other sites, so I would like to know exactly where to look to find the data.
I am thinking that stock websites would be a great example but I am having a tough time finding one that fits this example.
Thank you for your help!
回答1:
I just want to know how to locate data of a website that I do not have to log into in Safari via javascript
This can be easely done with JavaScript: with it you may get dom element(s) by class, name, id, traverse them upwards or inwards and get their properties like text and html content. Please tells us what have you tried and which error do you get: nobody can learn JavaScript for you.
and display the data in the results of Applescript Editor.
All you have to do to pass a variable from JavaScript to AppleScript is to include a return statement in your js code.
property myVar : missing value
tell application "Safari"
set myVar to do JavaScript "
var myVar = " & myVar & ";
myVar = myVar + 1;
return myVar;
" in document 1
end tell
display dialog myVar
The do JavaScript
command, which is specific to Safari, will return the same value returned by the script it executes. Hence, you may save any value from a webpage to your applescript.
来源:https://stackoverflow.com/questions/19487595/how-to-grab-html-snippet-via-applescript-with-javascript