Adding text to dialog field

I have been using a script that adds my info into text fields on a website I use daily using keystroke tab commands. But it is highly unreliable. If often repeats keystroke commands, or tabs over to many/few times and adds my dialog to the wrong field. I am trying to fine a way to use a:

do javascript () command

to add my info into the fields. I inspected the element of the first dialog field where I need to add my first name, and I posted the webpage source code below if that helps anyone. If I can get help with this, I can use it to write the rest of the script for the other dialog boxes. Thanks guys.

<fieldset>
<legend>Required Information</legend>
<div class="formRow clearfix">
<div class="formElement">
<label>First Name</label>
<input class="text" type="text" maxlength="30" size="25" name="fvFirst"/>
</div>

I’m not good with javascript commands but I use this for one of my text fields so maybe it will work for you. Just change theValue to whatever you want entered.

do JavaScript "document.forms[0]. fvFirst.value = \"" & theValue & "\"" in document 1

And I use this to submit the form after entering the values:

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

that looks like it should do it, but when I run it, i get the following error:

Expected end of line, etc. but found identifier.

Looking at my code above, it seems I have a space before the word “fvFirst”, so if you copy/pasted it in you’ll need to fix that. If that’s not the problem then you would have to post your code because the error could be somewhere else in your script.

I deleted the space and ran the following script:

set theURL to "http://www.mywebsite.com"
tell application "Safari"
	activate
	make new document with properties {URL:theURL}
	delay 10
end tell
tell application "Safari"
	do JavaScript "document.forms[0].fvFirst.value = \"" & myname & "\"" in document 1
end tell

I got the following error:

The variable myname is not defined.

then I tried setting a variable with the following:

set theURL to "http://www.mywebsite.com"
tell application "Safari"
	activate
	make new document with properties {URL:theURL}
	delay 10
end tell
set myName to "name"
tell application "Safari"
	do JavaScript "document.forms[0].fvFirst.value = \"" & myName & "\"" in document 1
end tell

It ran, but nothing was entered into the text field.

Yes, myname is a variable in this case because you have “&” characters on either side of it so you need to assign it a value. Unfortunatley if it’s not working I’m not sure what the problem is without knowing the website. Maybe the page has more than one form so try forms[1]??? Sorry I couldn’t be more help.

This will target the first field with a name value of ‘fvFirst.’ If there are more than one change the (0) incrementally until you find the one you are looking for.


set nameValue to "Your Name"
tell application "Safari"
	do JavaScript "document.getElementsByName('fvFirst')[0].value='" & nameValue & "';" in document 1
end tell

that did it. Thanks for your guys help.