More Safari, JavaScript and forms!

Here is a simple safari script that partially works.

tell application "Safari"
	do JavaScript "window.document.forms[3].elements[42].value='stuff';" in document 1
	do JavaScript "window.document.forms[3].submit();" in document 1
end tell

The first JavaScript works. It fills in a text input box with the value. But the “submit” script line does not. It only refreshes the page and the value in the input box reverts to what it was before the value change. So, am I doing something wrong or is there something in the HTML of the page that prevents me from submitting the new value to the form?

The page HTML of the “input” tag looks like this:

 <input type=submit value="Update Bids"  name="/go2/directraffic/handler/BidChangeHandler.bidAndTypeLB">

any help would be appreciated.

What about the form “action”? Could we take a look to the related page?

This is what is after the Form tag. Is this what you are looking for?

<form name=listingsForm method=POST action=/s/dtc/center/pb/index.jhtml;$urlparam$Q3bZiW0hmTvlxMkrq5bPLTUvPMejFAntKVtMsRHigL2D79zboVTn9YcNaB?_DARGS=/base/pb/pbtable.jhtml.5>

I could give you all the form tag contents, if you request


Whow! Strange action! :lol:
I don’t know if it would be useful see the entire form… When you fill and submit the form by hand, do you follow the same steps than in AppleScript (that is “fill text field x, then submit”), and it works fine?

What does it happen if you use the same code in Internet Explorer?

tell application "Internet Explorer" 
   do script "window.document.forms[3].elements[42].value='stuff';"
   do script "window.document.forms[3].submit();"
end tell

The IE script doesn’t work.

When I fill out the form manually, and hit submit, it works properly. When I use the safari applescript, I can physically see it change the value iin the text field and the the page refreshes as in a form submit, but the value reverts back to the prevous value in the field. So if there was “1.25” in the field and the script changes the value it to “1.65” and then refreshes the page, the result in the field is the orginal value of “1.25”.

Is there a way to submit or click the button with System Events and GUI scripting of OSX 10.3?

Yes. Your situation is hard, though. They have a dot in the NAME of the submit button. It should still let you use submit(), but I’ve seen that fail before. Normally you’d use this:


One problem, though is that you cannot use the name if the name includes a dot. So, you’ll have to find out which element the submit button is, numerically. In other words, you can do:


if the submit button is the 7th element of the form’s object (remember that JavaScript starts at 0 for counting objects). So, you’ll need to find which element the submit button is, and use that.


will tell you which element is [6], and you can find out the right number.

Sorry if that is somewhat confusing, but it’s a tricky thing to deal with when names of objects contain a dot, and you want to use JavaScript to refer to them.

Can you give me an example of GU scripting and system events to get me going in the correct direction. I found that the button is the 20th element in the form.

No, no. This is NOT GUI scripting (at least, not Apple’s GUI scripting). It is accessing the objects via JavaScript. I was suggesting that you find out what element the submit button is of the JavaScript object, and reference it that way to send a click() event directly to the button, since the submit() call did not work. If the submit button is the 20th element of the form (from Javascript’s point of view) then the code you need to mimic clicking the submit button would be:

tell application "Safari" 
   do JavaScript "window.document.forms[3].elements[19].click();" in document 1
end tell

Note that the 20th object is elements[19], since elements[0] is the first element of the form.

Let us know if that works to submit the form.

No, it does nothing. I’m sure I have the correct element because if I change the value of the element in Javascrip by:

t “do JavaScript :“window.document.forms[3].elements[20].value=‘test’;” in document 1”
the name of the button changes to “test” on the form. I have also gone through all element numbers on the form and still nothing happens.

So what do you think I’m doing wrong or is there somethnig on the form that prevents this kind of automation?


Hmm, well you tested it well. If the name changes, then you are obviously referencing it correctly. Not sure why it won’t respond to the click() event. I’m not sure how they could prevent this. Is this a public form that I could look at? Perhaps I could see what is happening?

No, it’s not public and I could not get you to it because it contains sensitive information. I did solve the problem with GU scripting.

tell application "Safari" to activate
tell application "System Events"
	tell process "Safari"
		tell window "Overture - DirecTraffic Center"
			tell group 3
				tell scroll area 1
					click button "Update Bids"
				end tell
			end tell
		end tell
	end tell
end tell

This works. I don’t know why the other method does not.

Glad you solved it. Strange, though.