PDA

View Full Version : click button on website




thriftinkid
Aug 9, 2009, 03:50 PM
Hey guys. I have a website I need to fill out my personal info on. I have the following script to add my info into text fields:

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

I have to select my gender by clicking "male"

I don't know the Javascript code to do so. I posted the source code from the page below if it helps anyone. Thanks so much.

<h6>Gender</h6>
<input type="radio" value="m" name="fvGender"/>
<label class="radio">Male</label>
<input type="radio" value="f" name="fvGender"/>
<label class="radio">Female</label>
</div>
</div>



SRossi
Aug 9, 2009, 03:53 PM
Have a look at this post (http://forums.macrumors.com/showthread.php?t=184247) :). Not sure if it will help thought.

Stephen

Sijmen
Aug 9, 2009, 04:13 PM
If you can get the form with getElementById() or getElementsByTag(), you can call submit() (http://www.w3schools.com/htmldom/met_form_submit.asp) on it.

In case that's not what you meant, please tell :)

angelwatt
Aug 9, 2009, 04:53 PM
Give the input elements ID attributes, then you can do,
document.getElementById('fvGenderM').checked = true;

Or, since this is only going to be run in Safari,
document.getElementsByName('fvGender')[0].checked = true;

thriftinkid
Aug 9, 2009, 07:19 PM
Give the input elements ID attributes, then you can do,
document.getElementById('fvGenderM').checked = true;

Or, since this is only going to be run in Safari,
document.getElementsByName('fvGender')[0].checked = true;

Your second option did it. it's written as follows for those in need:

tell application "Safari"
do JavaScript "document.getElementsByName('fvGender')[0].checked = true;" in document 1
end tell

Thanks Again.

thriftinkid
Aug 9, 2009, 07:41 PM
I want to click the submit button now, but the following code doesn't seem to be working. What am I doing wrong here?

Source page:

name="submitButton"/>

I tried the following:

tell application "Safari"
do JavaScript "document.getElementsByName('submitButton')[0].click()" in document 1
end tell

and

tell application "Safari"
do JavaScript "document.getElementsByName('submitButton')[0].onclick()" in document 1
end tell

and

tell application "Safari"
do JavaScript "document.getElementsByName('submitButton')[0].submit()" in document 1
end tell

Any ideas?

angelwatt
Aug 9, 2009, 08:03 PM
Try using a name on the form where you have some action set.
<form name="theform" action="somepage.php" method="post">
...
</form>

Then, for JavaScript,
document.getElementsByName('theform')[0].submit();

It may also work to use,
document.theform.submit();

thriftinkid
Aug 9, 2009, 08:13 PM
got form id and used this guy and it worked. Thanks!

document.getElementsByName('theform')[0].submit();