click button on website

Discussion in 'Mac Programming' started by thriftinkid, Aug 9, 2009.

  1. thriftinkid macrumors regular

    Joined:
    Mar 24, 2008
    #1
    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:

    Code:
    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.

    Code:
    <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>
     
  2. SRossi macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #2
    Have a look at this post :). Not sure if it will help thought.

    Stephen
     
  3. Sijmen macrumors 6502a

    Joined:
    Sep 7, 2005
    #3
    If you can get the form with getElementById() or getElementsByTag(), you can call submit() on it.

    In case that's not what you meant, please tell :)
     
  4. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #4
    Give the input elements ID attributes, then you can do,
    PHP:
    document.getElementById('fvGenderM').checked true;
    Or, since this is only going to be run in Safari,
    PHP:
    document.getElementsByName('fvGender')[0].checked true;
     
  5. thriftinkid thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #5
    Your second option did it. it's written as follows for those in need:

    Code:
    tell application "Safari"
    	do JavaScript "document.getElementsByName('fvGender')[0].checked = true;" in document 1
    end tell
    Thanks Again.
     
  6. thriftinkid thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #6
    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:

    Code:
    name="submitButton"/>
    I tried the following:

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

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

    Code:
    tell application "Safari"
    	do JavaScript "document.getElementsByName('submitButton')[0].submit()" in document 1
    end tell
    Any ideas?
     
  7. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #7
    Try using a name on the form where you have some action set.
    HTML:
    <form name="theform" action="somepage.php" method="post">
    ...
    </form>
    Then, for JavaScript,
    PHP:
    document.getElementsByName('theform')[0].submit();
    It may also work to use,
    PHP:
    document.theform.submit();
     
  8. thriftinkid thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #8
    got form id and used this guy and it worked. Thanks!

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

Share This Page