Do JavaScript Help

I’m trying to use the do Javascript command to look up ip addresses. My script works fine with loggint in my name and password, and filling out the IP Query field with the desired IP address, but what I can’t seem to do is to get the page to submit my query. I don’t know anything about Javascript so I’m hoping I’m just making a newbie mistake here. Any suggestions?

tell application "Safari"
	
	open location "http://www.ip-adress.com"
	
	delay 3
	do JavaScript "document.forms[0].login.value = \"" & "MYEMAIL@gmail.com\"" in document 1
	do JavaScript "document.forms[0].password.value = \"" & " MYPASSWORD\"" in document 1
	do JavaScript "document.forms[0].submit()" in document 1
	delay 6
	
	do JavaScript "document.forms[0].QRY.value = \"" & "66.249.66.112\"" in document 1
	
	do JavaScript "document.forms[0].submit()" in document 1
	
end tell

So the first do javascript submit code works, because that logs me into with my name and password. It’s the second submit code that isn’t working. Nothing happens, my script just fills in the IP Query text field with “66.249.66.112”

I noticed something strange about this site … when you enter an ip address to search, the url displays something like this:

That last part looks like some sort of encryption. When you view the page source, that encrypted string is labeled as the “form action” for the IP address search query textbox. Is this why my script is having trouble? If so, how do I get around that (without enabling UI scripting)?

Check to see if there may be more than one form on that page. “document.forms[0].submit()” would only work if the IP field were attached to the first form on the page.

Hi Kevin. There is more than one form, and so I tried

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

and for X, I tried 1-10, and each time, nothing happened. Is that the right way to distinguish between multiple forms?

I checked that, and each time I loaded the page, that string changed! So I doubt it is the IP address encrypted, more likely it is a transaction code designed specifically to prevent someone from doing what you are trying to do - use their lookup in some kind of automated way. When the page is created, the server creates that code string and looks for it to be submitted with the form submit, then compares it to the original to verify. One way to tell would be to save the html of that page, change the code, then try to submit using the saved HTML page. I’m betting it won’t work.

That being said, you can probably still use System Events to submit the page.

I was afraid of that, Kevin. Thanks for the input though, it’s better I know now rather than after a week of trying. I’ll give it a try using System Events … I don’t know why, but that always feels like cheating. Oh well.

Heh, a couple keystroke tabs followed by a keystroke username and keystroke password with a keystroke return never hurt anybody :wink: I use said method currently and it gets the job done since I don’t know jack about javascript >.<