I’ve written a script to login to my webmail but instead of using JavaScript it uses “Keystroke” to input the username and password. Although this does work 90% of the time it’s annoying that it doesn’t work 100%!!
I’m thinking the best way to fix the script is to use JavaScript instead. So can anyone give me the basic code I need to use?
set x to "document.forms[1].elements[21].value = 'yahoo_id';
document.forms[1].elements[22].value = 'password';
document.forms[1].submit();"
tell application "Safari" to do JavaScript x in document 1
But this will depend on the specific page… If you post here your webmail login page, we could help much better.
set x to "document.forms[0].elements[1].value = 'xxxxxxx';
document.forms[0].elements[2].value = 'yyyyyy';
document.forms[0].elements[3].value = 'zzzzzzz';
document.forms[0].submit();"
tell application "Safari" to do JavaScript x in document 1
set x to "document.forms[0].elements[0].value = 'user-user';
document.forms[0].elements[2].value = 'user-domain';
document.forms[0].elements[3].value = 'mailserver';
document.forms[0].elements[4].value = 'pwd';
// uncomment the following line if you need the advanced interface
// document.forms[0].elements[5].selectedIndex = 1;
document.forms[0].submit();"
tell application "Safari" to do JavaScript x in document 1
Note, that I’ve changed only the “elements” index (now: 0, 2, 3, 4). This is the positional index of the input elements in the form, being “0” the first one (and “forms[0]”, the first form in the document).
The fifth element, the popup menu, must be althered using its “selectedIndex” property.
set x to “document.forms[0].elements[0].value = ‘username’;
document.forms[0].elements[1].value = ‘password’;
document.forms[0].submit();”
tell application “Safari” to do JavaScript x in document 1
I’m a perfect noob and this is my first post, so please don’t hit me
it would be nice if you have a minute for me too. are you able to help me?
bye
Model: ibook
Browser: Safari 412
Operating System: Mac OS X (10.4)
Advice for folks with this kind of problem: if you are not able to read and understand HTML code, keep trying elements “positionally”. For example, your script would work fine with “elements[1]” for the username field, and “elements[2]” for the password one.
If, finally, you can’t see your data entered in such fields, perhaps you must try with “forms[1]” (the second form in the HTML page), and so on…
If it still doesn’t work, you perhaps found a page with frames, iframes or other issues. Then, feel free to ask here (or perhaps better in a javascript forum :rolleyes: )
Enjoy the mix of AS and JS!
This has been an immensely useful topic.
My ideal application is this:
Create an automator action to download monthly pdf statements from all my online banking accounts. Since, automator by itself is not very capable, I believe applescript has to be used in conjunction with automator.
I have now arrived a the login part - and that is where I seem to be stuck:
From reading this forum, I have tried two methods. One using System Events keystrokes and the other like in this thread, using JavaScript.
Method 1:
use automator to go to the website and then execute this script -
(note: my log in ID is saved in safari and is pre-filled, only my passwd needs to be entered)
set x to "document.forms[1].submit();
tell application "Safari"
activate
delay 3
tell application "System Events"
keystroke tab
keystroke "xxxxxx"
end tell
do JavaScript x in document 1
end tell
This ends in a failed login (not wrong login) with a text that says, “Unable to log you in”
Method 2:
After reading this thread I tried out the following script:
set x to "document.forms[1].elements[4].value = 'xxxxxx';
document.forms[1].submit();"
tell application "Safari" do JavaScript x in document 1
end tell
This fails with a message saying that “The saved ID is different from the ID you just entered”. I tried varying the elements[] value from 1 to 7 with both my login ID and password. I also tried two separate statements for my login and password. Still no cigar.
Any help will be appreciated. Specifically - how do you figure out the value that goes into the forms[] section, and the elements[] section.
thanks
Indy
Model: iMac G5 1.8
AppleScript: editor 2.1
Browser: Safari 412
Operating System: Mac OS X (10.4)
Just using the “trial and error” method, I think this could work for you:
set x to "document.forms[1].elements[13].value = 'xxxxxx';
document.forms[1].submit();"
tell application "Safari" to do JavaScript x in document 1
This is a more complex page, so…
Go to the related page and browse its source (apple + option + “v” → or “u”, depending on your Safari’s version)
Hit apple + “f” and search for “<form” (no quotes). You should find your form easilly taking a look to the “action” tag of such form. In “bankofamerica.com”, the first form’s action is “/search/”. We don’t need this. We need the second one, as you guessed (“forms[1]”), labeled “frmSignIn”.
Count the form elements in such forms till you reach your main goal. You must take in consideration any form element: hidden fields, selects, textareas, input-type-text, etc. Usually, you won’t need “hidden” fields for this kind of task, so just count them and ignore them (even when they are called “Current_Passcode” or “pswd”). In this particular example, you can search for known attributes. This is a password field, and you can search for <type=“password”>. Or you can search for the visual string “Enter Passcode:”, and you will find the form element very close to this text.
Lots of times the “trial and error” method is faster. Just keep trying from “elements[0]” thru “elements[25]”, and you will find very soon your field filled.
You can also use the “names” of the things. This is the equivalent of the code posted above:
set x to "document.forms['frmSignIn'].elements['pc'].value = 'xxxxxx';
document.forms['frmSignIn'].submit();"
tell application "Safari" to do JavaScript x in document 1
You will find that the “name” tag of the related form is “frmSignIn”, and the pwd element is called “pc”. So, you can target objects by name (appart from positionally).
One more thing: this is a javascript-related issue. I think learning a bit of javascript is a must-have if you are a coder and you use HTML documents in a way or other in your daily life. Simply learning the DOM is very easy (that is, a “form element” is owned by a “form”, which is owned by a “document”), very similar to AS (an “item” inside a “folder”, inside an “application”, for example). Then, pick your favorite javascript reference, and you are done (just in case you need access more complex objects, or call the “focus()” function for a text field, resize a window, etc.).
What got me was that after going to elements[7], I figured I must be doing something wrong and then tried various other combinations. Also, thank you for the tip that we should also count the “hidden” values.
I figured out how to count the forms[] values. The elements[] value got me.
I also like your idea of using names of the forms rather than addressing them positionally. It seems more elegant and it should still work of other form elements are added/deleted.
Ultimately I would like to release this as a freeware application - with modules for different banks. When you combine this automator action with iCal - it will be really cool to have all your monthly statements automatically downloaded, renamed and saved. Thanks for helping me get a great start with this. “jj”.
property target_URL : "http://www.juris.de/jportal/Zugang.jsp"
tell application "Safari" to open location target_URL
set x to "document.forms[1].elements[6].value = 'your login without quotes';
document.forms[1].elements[7].value = 'your password without quotes';
document.forms[1].submit();"
tell application "Safari" to do JavaScript x in document 1
hey everybody,
i tried the scriptlet above written for yahoo mail and it worked great. however i mostly use yahoo japan’s mail (mail.yahoo.co.jp) and it didn’t work. i tried adjusting the numbers but nothing i tried did anything. could someone give me an idea on how to adjust the scriptlet for this homepage?
thanks,
greg
Model: ibook
AppleScript: 1.9.3
Browser: Safari 312
Operating System: Mac OS X (10.3.9)
So fine, indy
For Yahoo Japan, I’d say it’s forms[0], and elements 22 and 23. Remember, browse page source, locate the related form searching for “<form”, then count elements (inputs, textareas, etc) till you find the ones you need. Or just call’em by name, which may be more interesting, just in case the folks add new fields or interchange their order. Ie: “document.forms[‘login_form’].elements[‘login’].value = ‘foo’;”
i must be thick. i tried it both ways using 0,22,23 and using names and neither worked. i also tried counting form myself, but didn’t have any luck with that either. what am i doing wrong?
it turns out that i am stupid. i retried the suggestions you made in a new script window and this time it worked. unfortunately, i trashed the old script, so i can’t figure out what i did wrong.
one more thing: i am new to applescripting (and programming). i got into it just because i thought it would be cool to make use of this piece of included software and it has proved useful. but, it seems that applescript is mostly for automating repetetive tasks. i would like to make use of pieces of software and to make different pieces software work with each other (for example, using the dvd player and airtunes together, and so on). what would you recommend?