Javascript to Login to Macscripter Webpage

Firstly, A Safe and Happy New Year! :slight_smile:

I am quite new to Applescript but I have been tinkering with it over my break.

I am trying to understand using Javascript to login to a webpage. I have tinkered with logging in to the Macscripter website but I’m finding difficulties understanding the Javascript tags.

Here is what I have using Javascript to login to the Mascripter webpage.

set loginurl to "http://macscripter.net/login.php"
set mylogin to "Username"
set mypassword to "Password"
tell application "Safari"
	make new document at end of documents
	set URL of document 1 to loginurl
	--This subroutine makes sure the page has finished loading -- it expects the page to finish with the "</html"> tag
	my waitforload()
	--now that the page has loaded, we use Javascript inside Safari to fill in the form and click the submit button!
	do JavaScript "document.forms[login][req_username].value " = mylogin in document 1
	do JavaScript "document.forms[login][req_password].value " = mypassword in document 1
	do JavaScript "document.forms[login].elements[1].click()" in document 1
end tell
on waitforload()
	--check if page has loaded
	set loadflag to 0
	repeat until loadflag is 1
		delay 0.5
		tell application "Safari"
			set test_html to source of document 1
		end tell
		try
			set zarg to text ((count of characters in test_html) - 10) thru (count of characters in test_html) of test_html
			if "</html>" is in text ((count of characters in test_html) - 10) thru (count of characters in test_html) of test_html then
				set loadflag to 1
			end if
		end try
	end repeat
end waitforload

The result I get is:

do JavaScript false in document 1
do JavaScript false in document 1
do JavaScript “document.forms[login].elements[1].click()” in document 1

Obviously, I am incorrect. Where is the error and how to fix it?

If someone can explain to me the correct procedure to identify the document.forms[form name] and the correct syntax I would be most appreciative.

Cheers,

Rob

Model: iMac
AppleScript: 2.3
Browser: Safari 533.19.4
Operating System: Mac OS X (10.6)

Hi Rob,

I have only made minor changes to your existing code, but now it works for me. Still, the waitforload handler seems to be a bit unreliable. Hope it helps :slight_smile:


set loginurl to "http://macscripter.net/login.php"
set mylogin to "Username"
set mypassword to "Password"

set logincode to "document.forms[0].elements[2].value = '" & mylogin & "'"
set pwcode to "document.forms[0].elements[3].value = '" & mypassword & "'"

tell application "Safari"
	make new document at end of documents
	set URL of document 1 to loginurl
	--This subroutine makes sure the page has finished loading -- it expects the page to finish with the "</html"> tag
	my waitforload()
	--now that the page has loaded, we use Javascript inside Safari to fill in the form and click the submit button!
	do JavaScript logincode in document 1
	do JavaScript pwcode in document 1
	do JavaScript "document.forms[0].elements[4].click()" in document 1
end tell

on waitforload()
	--check if page has loaded
	set loadflag to 0
	repeat until loadflag is 1
		delay 0.5
		tell application "Safari"
			set test_html to source of document 1
		end tell
		try
			set zarg to text ((count of characters in test_html) - 10) thru (count of characters in test_html) of test_html
			if "</html>" is in text ((count of characters in test_html) - 10) thru (count of characters in test_html) of test_html then
				set loadflag to 1
			end if
		end try
	end repeat
end waitforload

Best regards from pretty cold Berlin,

Martin

Thanks for your reply Martin!

I refined the script and now it works. :wink:

set loginurl to "http://macscripter.net/login.php"
set mylogin to "Username"
set mypassword to "Password"
tell application "Safari"
	make new document at end of documents
	set URL of document 1 to loginurl
	delay 3
	--now that the page has loaded, we use Javascript inside Safari to fill in the form and click the submit button!
	do JavaScript "login.req_username.value = \"" & mylogin & "\"" in document 1
	delay 1
	do JavaScript "login.req_password.value = \"" & mypassword & "\"" in document 1
	delay 1
	do JavaScript "login.login.click()" in document 1
end tell

Cheers,
Rob