Using applescript to filed a webform in safari

Hi,

I need some help creating a applescript . I’m trying created a script to fill out web form such like this http://www.asp101.com/samples/checkbox.asp. The webform is only checkboxes.

I will really appreciate if anybody can help me with this or if you can point me to where I can get information about how to do it I will appreciate.

Thanks

I would search around for GUI scripting articles, but to tell you the truth its not pretty. Using UI Brower from the PreFab group to click the very first box this works…

activate application "Safari"
tell application "System Events"
	tell process "Safari"
		click checkbox 1 of group 36 of UI element 1 of scroll area 1 of group 4 of window "ASP 101 - Checkbox Sample"
	end tell
end tell

To click the other checkboxes just change the checkbox number… the sad thing though is this may or may not be the case on every webpage.

Hi,

in your example the first checkbox can be set with

tell application "Safari"
	do JavaScript "document.forms[3].elements[0].checked=true" in document 1
end tell

but don’t ask me how to determine a certain element, I’ve done it trial&error with
Script Debugger’s single step function and this snippet

tell application "Safari"
	repeat with formIndex from 0 to 10
		repeat with elementIndex from 0 to 10
			do JavaScript "document.forms[" & formIndex & "].elements[" & elementIndex & "].checked=true" in document 1
		end repeat
	end repeat
end tell

Edit: You can determine the form element by name with


set checkBoxName to "default"

tell application "Safari"
	set maxForm to do JavaScript "document.forms.length" in document 1
	repeat with formIndex from 0 to maxForm as integer
		set maxElements to do JavaScript ("document.forms[" & formIndex as string) & "].elements.length" in document 1
		repeat with elementIndex from 0 to maxElements
			if (do JavaScript "document.forms[" & formIndex & "].elements[" & elementIndex & "].name" in document 1) is checkBoxName then
				display dialog "FormIndex: " & formIndex & " / ElementIndex: " & elementIndex
				do JavaScript "document.forms[" & formIndex & "].elements[" & elementIndex & "].checked=true" in document 1
			end if
		end repeat
	end repeat
end tell

Stefan’s a cheater though… using JS :stuck_out_tongue:

the worst thing is better than GUI scripting :wink:

Amen. :lol:

Thats not the point :stuck_out_tongue:

3 more questions:

1.-how you load the url on applescript at the moment you open Safari?
2.-using Tell you open the application but how you close the application with applescript?
3.-applescript has some kind of for loops?

Hi,

1. this is a way tp open a page with a subroutine, which waits until the page is reliably loaded

tell application "Safari"
	activate
	open location "http://www.asp101.com/samples/checkbox.asp"
	if page_loaded(20) is false then display dialog "Couldn't load page" buttons {"Cancel"} default button 1
	-- do something
end tell


on page_loaded(timeout_value)
	delay 2
	repeat with i from 1 to timeout_value
		tell application "Safari"
			if (do JavaScript "document.readyState" in document 1) is "complete" then
				return true
			else if i is timeout_value then
				return false
			else
				delay 1
			end if
		end tell
	end repeat
	return false
end page_loaded

2.

quit application "Safari"

3. Yes, repeat blocks in several modes

on this example:


set checkBoxName to "default"

tell application "Safari"
   set maxForm to do JavaScript "document.forms.length" in document 1
   repeat with formIndex from 0 to maxForm as integer
       set maxElements to do JavaScript ("document.forms[" & formIndex as string) & "].elements.length" in document 1
       repeat with elementIndex from 0 to maxElements
           if (do JavaScript "document.forms[" & formIndex & "].elements[" & elementIndex & "].name" in document 1) is checkBoxName then
               display dialog "FormIndex: " & formIndex & " / ElementIndex: " & elementIndex
               do JavaScript "document.forms[" & formIndex & "].elements[" & elementIndex & "].checked=true" in document 1
           end if
       end repeat
   end repeat
end tell

where applescript is clicking “submit” or how can I click submit?

try this


do JavaScript "document.forms[" & formIndex & "].submit()" in document 1

One more question. in a web form for applescript is a difference between a checkbox and radio buttons or can I use the same syntax?

I think so, I have no idea about javascript,
I got the informations above by reading a javascript reference