![]() |
applescripting a javascript form
I have been scouring the forums trying to figure out how to change a popup box in a javascript web form but haven't had any luck. It's driving me crazy.
There is a webpage that contains a form (in a frame) that I need to set to a certain value. The webpage is on an internal network, so I can't post a URL to the page, unfortunately. One of my issues, is that I can't figure out how to know what the index # is for the form or for the elements. I have tried scripting it via name, but that isn't working either. Here is what I have been trying, changing things around a little and trying again, but it's not working. tell application "Safari" -- go to appropriate URL to login open location "url" delay 8 do JavaScript "window.frames[1]document.forms['locations']['LocationName'].option.selectedIndex = 'Honolulu'" in document 1 end tell The html source code for the frame is this <HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> <TITLE></TITLE> </HEAD> <SCRIPT LANGUAGE="JavaScript"> function GetRecords() { document.locations.action = "LocationPXS.asp"; document.locations.target = "LocationPXS"; document.locations.method = "POST"; document.locations.submit(); } function AddNew() { var c = document.getElementById('LocationName'); var w = c.selectedIndex; var selected_text = c.options[w].text; if (selected_text == '--Select--') { alert('Please select your location'); return false; } document.locations.action = "AddPX.asp"; document.locations.target = "LocationPXS"; document.location.method = "Post"; document.locations.submit(); } function GetData() { document.locations.action = "LocationPXS.asp"; document.locations.target = "LocationPXS"; document.locations.method = "POST"; document.locations.submit(); } </SCRIPT> <BODY ONLOAD="GetData()" Language="JavaScript"> <FORM NAME="locations" METHOD=POST ACTION="LocationPXS.asp"> <CENTER> <IMG src=top.gif><P><Table border =0><TR><TD>Please Select Your Location </TD><TD><SELECT NAME=LocationName OnChange="GetRecords()"><OPTION>--Select--</OPTION><OPTION>--- long list of options -- </SELECT></TD></TR></Table> <INPUT TYPE=BUTTON OnClick="AddNew()" NAME=AddPX Value="Add PX"> </CENTER> </FORM> </BODY> </HTML> Any help would be greatly appreciated. Thanks djeans |
First help I would offer is build in stages - don't add Javascript until the plain form is behaving properly. And a lot of the JS code appears redundant - you don't need JS to simply submit a form, just add the validation handler to the onSubmit action.
Second is don't use Applescript to alter web browsers - it's just not the right tool. Third, your Javascript isn't going to do anything until you put some id="" attributes in the html. You _can_ do it without, but it's a lot less stress with an id in there. It looks like you are in the early stages of updating part of the page on a select change? |
Not sure I understand, but maybe I should have been more clear about what I'm trying to do.
The web page is on a company intranet site that I have to open every morning and change a drop down box to my city "Honolulu", upon which the form will update the page with information specific to my location. I'm just looking for a way to script / automate this process with Safari, and have it email me the results automatically, so that the information is in my inbox when I wake up. I have found other applescripts about automating javascript forms from other sites, but haven't been able to alter them to fit my needs. Was hoping someone could help me out. D |
System Events may work
Welcome to javascript. Unless you can figure out the documents/elements, it's very difficult.
My advice for now would be to use System Events. You activate this by going to Universal Access and clicking the 'Enable access for assistive devices' Once that's click, go ahead and do something like the following: Code:
tell application "System Events" |
Thanks for the reply.
I was hoping to avoid UI scripting if possible. If that turns out to be the only way, how do I select my city from the list once I have system events click the pop-up? Thansk Darron |
An easy way to do this is to first learn some examples from the Apple website:
http://www.apple.com/applescript/uiscripting/index.html After you feel comfortable with how it works, then go to the following directory to find the Accessibility Inspector: To get to the path, you will need to installed the Developer package for this if you haven't done so already. The path is /Developer/Applications/Utilities/"Accessibility Tools"/"Accessibility Inspector.app" From there, it will tell you exactly what all the attributes are for a particular element. Write your script and set it to run whenever you like. |
I have tried the UI scripting, and have had some success, except where it comes to selecting the right item from the list.
tell application "Safari" activate tell (make new document) to set URL to "url" delay 5 tell application "System Events" to click pop up button 1 of group 3 of UI element 1 of scroll area 1 of UI element 1 of scroll area 2 of UI element 1 of scroll area 1 of group 3 of window "Main" of process "Safari" clicks the pop up button, but I haven't discovered the consistent way to select the right item in the list. I have tried using keystroke with the first couple of letters, and it works sometimes, but not always. Can you suggest a better way to select the right item? Thanks. D |
Quote:
Code:
tell application "System Events" |
Thanks,
but I'm still at a loss for how to change the value of that pop up. Darron |
Have you been able to use the Accessibility Inspector? It should be able to pick out the value of the pop up rather easily. Clicking the value would look something like the following:
Code:
click pop up button 2 of group 1 of scroll area 1 of group 2 of window "name_of_window" |
I'd like to point out another possibility (for achieving the higher-level goal) - use "Selenium": http://seleniumhq.org/
Quote:
|
Quote:
|
Thanks hayne, sounds like a good find. I will have to try it out one day.
|
I tried using the accessibility inspector, but it's not very clear. I have also tried UI Browser, which is slightly easier to understand.
Here is the Accessibility Inspector window for that popup <AXApplication: “Safari”> <AXWindow: “Main”> <AXGroup> <AXScrollArea> <AXWebArea: “”> <AXScrollArea> <AXWebArea: “”> <AXScrollArea> <AXMenu> <AXMenuItem: “Honolulu”> Attributes: AXRole: “AXMenuItem” AXRoleDescription: “menu item” AXParent: “<AXMenu>” AXEnabled: “1” AXPosition: “x=827 y=738” AXSize: “w=169 h=15” AXTitle: “1136-Honolulu” AXHelp: “(null)” AXSelected (W): “1” AXMenuItemCmdChar: “(null)” AXMenuItemCmdVirtualKey: “(null)” AXMenuItemCmdGlyph: “(null)” AXMenuItemCmdModifiers: “0” AXMenuItemMarkChar: “ |
Quote:
I installed the plugin, and i was able to record and playback going to the page and selecting the menu item, but I'm not sure how to incorporate that into an applescript. D |
Well, you would put it in a script other than applescript. Your Mac has python so you could put the code there and run it.
|
Quote:
|
Ok, here it is.
It gets to the page and clicks the pop up correctly, but stalls at selecting "Honolulu" and throws up an error that says "Invalid Index" tell application "Safari" activate tell (make new document) to set URL to "https://intranet.locations/" delay 5 tell application "System Events" to click pop up button 1 of group 3 of UI element 1 of scroll area 1 of UI element 1 of scroll area 2 of UI element 1 of scroll area 1 of group 3 of window "Main" of process "Safari" tell application "System Events" click menu item "Honolulu" of menu 1 of pop up button 1 of group 3 of UI element 1 of scroll area 1 of UI element 1 of scroll area 2 of UI element 1 of scroll area 1 of group 3 of window "Main" of process "Safari" end tell end tell |
Any ideas why the script is giving me the invalid index error?
|
According to the accessibility inspector, Honolulu is not in the pop up, i.e. the Invalid Index error: You might want to do something like the following although I'm not sure if it will work. Again, without having access to the website, it become a process of trial and error. You may want to put some effort into learning python if you are not able to get this to work for you:
Code:
tell Application "System Events" |
error seems to be coming from the get menu part
"System Events got an error: Can’t get menu 1 of pop up button 1 of group 3 of UI element 1 of scroll area 1 of UI element 1 of scroll area 2 of UI element 1 of scroll area 1 of group 3 of window . . . . " Tried the shorter script above, but it won't compile, with the error "Expected end of line but found identifier." at the web area part with area highlighted. I found that if I put in a long enough delay to select a location manually and then add in get value of pop up button 1 of group 3 of UI element 1 of scroll area 1 of UI element 1 of scroll area 2 of UI element 1 of scroll area 1 of group 3 of window "Main" of application process "Safari" It returns whichever location that I selected. I tried changing the get value to set value, but that had no effect. D |
Partial success:
I couldn't get anything to work, so I thought about a different approach. Using a loop and down arrow to scroll through all of the locations until I hit the right one. Here is the script so far set menu_item to "Honolulu" as string (* my location *) tell application "Safari" activate tell (make new document) to set URL to "https://intranet.locations/" delay 5 tell application "System Events" repeat until value of pop up button 1 of group 3 of UI element 1 of scroll area 1 of UI element 1 of scroll area 2 of UI element 1 of scroll area 1 of group 3 of window "Main" of application process "Safari" is menu_item tell application "System Events" to click pop up button 1 of group 3 of UI element 1 of scroll area 1 of UI element 1 of scroll area 2 of UI element 1 of scroll area 1 of group 3 of window "Main" of process "Safari" key code 125 (* down arrow *) keystroke return (* confirm choice of menu item *) delay 0.5 end repeat beep end tell end tell This works, albeit very slowly. For some reason, there is a long pause after the popup button is pressed (about 5 seconds) before it arrows down to the next selection even though there is no delay statement between the two actions. If you see anything I can change to clean up the code to make it faster, let me know. The next task will be getting the information that is returned in table form copied into an email - if you have any suggestions in that regard :) D |
I don't understand why you're UI scripting this when it's much easier to use Safari's Do Javascript command. write a short javascript in an applescript variable and fire it off, like the following:
Code:
Set JScode to "themenu = document.getElementById('LocationName'); |
tw,
thanks for the reply. I am not very familiar with javascript, but I did try dropping in your script, but it didn't appear to do anything. Does in need to have a dojavascript in front of it? Originally I was trying to avoid UI scripting in favor of javascript, but was not having any luck. Thanks D |
it's really hard to debug these things without access to the code. did you check console, or the safari error log (if you have it enabled) to see if it's throwing any errors?
|
ok, a little playing around, and I realized that I had the reference form wrong. try this version instead:
Code:
set JScode to "themenu = window.frames[0].document.forms[0].LocationName |
Thanks again for the reply and trying to help.
I tried your new code, with same results. The page has 2 frames, as far as I can tell, with the only form on the page being in the second frame so I did change the frames[0] to frames [1] and tried that as well. Still nothing happening after the page loads. D |
Quote:
|
If you know who developed the web page, you might try asking the programming what the elements/documents are for what you are trying to do. Ask them, how do they access this with javascript.
|
Ok, looked at Safari error log, there is this:
TypeError: Result of expression 'window.frames[1].document' [undefined] is not an object. |
well, that's not very helpful. :D what does the html of the frames page look like? not the page you posted, which is what goes in the frame, but the outer page...
|
Ok, here is the html for the main page
<html> <head> <title>Main</title> </head> <frameset rows='70,*' border='0'> <frame src='/header.asp' name='Header' marginheight='0' marginwidth='0' scrolling='no' noresize> <frame src='http://intranet.locations/' name='Report' marginheight='0' marginwidth='0' scrolling='auto'> </frameset> </html> Hope that helps d |
alright, do this. copy the following code:
Code:
set JScode to "themenu = window.frames['Report'].document.forms['locations'].LocationName |
Quote:
A property can’t go after this “"”. with "Index " highlighted when trying to compile. Won't let me run the script. D |
Quote:
|
Ok,
Thanks! It compiles now, but when run, nothing happens. Error log returns the same error: TypeError: Result of expression 'window.frames['Report'].document' [undefined] is not an object. |
well, I don't know what to tell you. I duplicated the structure of the two pages you posted and the script works fine, so that tells me that something is off in what you sent, or some kind of DOM manipulation is happening in the javascripts that I can't see. the fact that it says the document is not an object tells me that that frame is not loaded when the script runs, but not why...
if it were me, I'd start playing with Safari's Web Inspector (right click on the menu and select 'Inspect Element') to see if I could figure out the correct way to reference the menu and scripts (which is where we're stuck). nothing more I can do on this end, though (except guess), without access to the site. |
I think I may have stumbled on something, although I'm not sure if it is the problem, or if it is, if it can be worked around.
found another error that says: Unsafe JavaScript attempt to access frame with URL https://extranet.mycompany.com/web/t.../px/locations/ from frame with URL http://www.mycompany.com/px/locations/Locations.asp. Domains, protocols and ports must match. I have edited out my company name out of paranoia, and I could perhaps provide you with a url, but don't want it on the forum, for same paranoia reasons. I appreciate your help thus far, and understand if you are not interested in helping further, but if you are, let me know and I'll give you a url to test it out via email or private msg. d |
you can send me a private message with the info on the forum, no problems. just hover your mouse over my user name and click from the auto-dropdown -menu.
|
TW, sent you a private message yesterday, but can't find it in my sent messages. Did you get it?
D |
Used Keystrokes in the AppleScript?
Have you tried using keystrokes and clicks with AppleScripts. I use it to login to Google Mail automatically:
http://applescripts.thompson-solutio...i_mac_os_x.php Reference Keystroke materials can be found below at: http://applescripts.thompson-solutio...oard_guide.php |
Quote:
You might want 5 seconds delay if you are hearing beeps. Once you get the scripts to work, you can reduce the delays. Code:
tell application "System Events"Be sure to spell the menu items correctly or else it will cause a beep. |
| All times are GMT -5. The time now is 05:35 AM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.