Applescript/Jave to "select" form method=post

Discussion in 'Mac Programming' started by aapl-danial, Jul 26, 2014.

  1. aapl-danial macrumors newbie

    Joined:
    Feb 8, 2013
    #1
    I'm messing with a script to select a form based off a "select" button, except the webpage could have several identical "select" buttons, and I need to always choose the last one. Here is the page source data for a webpage with 4 submit buttons, all named the same:

    And here is my javascript written in AS to try and get it.


    Code:
    	tell application "Safari"
    				do JavaScript "document.forms[contactform].submit();" in document 1
    			end tell
    			
    I'm wondering if there is a way to title each submit button 0,1,2... then another command to select the last one? I'm a bit stuck on this one. Thanks!
     

    Attached Files:

  2. aapl-danial thread starter macrumors newbie

    Joined:
    Feb 8, 2013
    #2
    It's gotta be something with lastChild correct? I'm super stuck on this one. Someone must know
     
  3. numero macrumors regular

    Joined:
    Jul 23, 2002
    Location:
    OR
    #3
    Try this:
    Code:
    tell application "Safari"
    	do JavaScript "document.contact.recipient.value = \"delqcoll@dekalbcountyga.gov\";" in document 1
    end tell
    This will give you come background on the technique.
    http://www.javascriptkit.com/javatutors/combos1.shtml

    In this case we don't have a simple 1, 2, 3 . . . values like the above link will show.

    Code:
    <select name="recipient" onchange="checkForm(this.form)" style="background-color:#D9E8FF">
      <option value="">Please select from the following...</option>
      <option value="motorveh@dekalbcountyga.gov">DeKalb County Vehicle Registration</option>
      <option value="proptax@dekalbcountyga.gov">DeKalb County Property Tax</option>
      <option value="delqcoll@dekalbcountyga.gov">DeKalb County Delinquent Property Tax</option>
    </select>
    Substitute whatever the VALUE is for the item you want in the AppleScript/Javascript you execute.

    I'm assuming you are working with a page something like:
    http://web.co.dekalb.ga.us/TaxCommissioner/Contact.asp
    If what you have is different then post the URL and I'll take a look.

    -numero
     
  4. 960design macrumors 68020

    Joined:
    Apr 17, 2012
    Location:
    Destin, FL
    #4
    Does the form submit button have an ID? If yes, it's an easy solution to use applescript to click the button with that ID.
     
  5. aapl-danial thread starter macrumors newbie

    Joined:
    Feb 8, 2013
    #5
    Nope no ID. I'll post the URL numero. Thanks!!!
     
  6. aapl-danial thread starter macrumors newbie

    Joined:
    Feb 8, 2013
    #6
    Thanks, I'm messing around with this one: https://paulding.paytaxes.net/customer/enhanced_property_tax_search.php

    Perform the search for say R053188 under PPIN, and the results below list several years. I always need to Select the final entry, or most recent year, 2013. Trouble is under source they're all the same name, form name and Select value. No ID.

    Then there's one like this: with only one entry. Hmm. Thanks!
    R032686
     
  7. numero macrumors regular

    Joined:
    Jul 23, 2002
    Location:
    OR
    #7
    Code:
    tell application "Safari"
    	do JavaScript "var len = document.contactform.length;document.contactform[len-1].submit();" in document 1
    end tell
    You ask the contactform array how many items it has. Then you perform the submit() function on the last one. The array starts at zero so the last form is the length minus 1.

    -numero
     
  8. aapl-danial, Jul 27, 2014
    Last edited: Jul 27, 2014

    aapl-danial thread starter macrumors newbie

    Joined:
    Feb 8, 2013
    #8
    Thanks! I'm still getting a "missing value" error in the replies as it skims over onto the next.

    here is my code:

    Code:
    set this_file to (((path to desktop folder) as string) & "TaxFile_Paulding.txt")
    
    set tabChar to tab
    
    set numbersFile to paragraphs of (read POSIX file "/Users/rickystrom/Desktop/propNumbers_Paulding.txt")
    
    tell application "Safari"
    	activate
    	make new document
    	set safariWindow to front window
    	
    	repeat with nextLine in numbersFile
    		if length of nextLine is greater than 0 then
    			
    			set the clipboard to nextLine
    			
    			set URL of tab 1 of safariWindow to "https://paulding.paytaxes.net/customer/enhanced_property_tax_search.php"
    			delay 2
    			tell application "System Events"
    				repeat 5 times
    					keystroke tab using option down
    				end repeat
    				delay 1
    				keystroke "v" using command down
    				delay 1
    				keystroke tab
    				key code 36
    				delay 8
    			end tell
    			
    			tell application "Safari"
    				do JavaScript "var len = document.contactform.length;document.contactform[len-1].submit();" in document 1
    			end tell
    			
    			
    			set pageText to text of current tab of safariWindow
    			
    			
    			
    		end if
    	end repeat
    end tell
    
    
    
    Again thanks for getting me this far! I didn't know two calls could be inside the JS

    EDIT: Geez added a small delay after the do javascript and it's working. weird.
     
  9. numero macrumors regular

    Joined:
    Jul 23, 2002
    Location:
    OR
    #9
    I think you may find that for properties with a single owner (a single contactform form) you will get an error with the javascript. When there isn't an array of contactforms I was getting 10 returned as the length. I don't understand why. I also noticed when there were multiple owners it doesn't matter which one is submitted. You always get the payment records for all owners. So my solution was to try to click on the first contactform (contact form[0]). That will work if there is more than one owner listed. If that errors out then there must just be one button so call it without the index value.

    If you have it running on your system then let it ride. If you end up having the same issue that I did then here is your fallback code.

    Code:
    set this_file to (((path to desktop folder) as string) & "TaxFile_Paulding.txt")
    
    set tabChar to tab
    
    set numbersFile to paragraphs of (read POSIX file "/Users/numero/Desktop/propNumbers_Paulding.txt")
    
    tell application "Safari"
    	activate
    	make new document
    	set safariWindow to front window
    	
    	repeat with nextLine in numbersFile
    		if length of nextLine is greater than 0 then
    			
    			set the clipboard to nextLine
    			
    			set URL of tab 1 of safariWindow to "https://paulding.paytaxes.net/customer/enhanced_property_tax_search.php"
    			delay 2
    			tell application "System Events"
    				repeat 5 times
    					keystroke tab -- using option down
    				end repeat
    				--delay 1
    				keystroke "v" using command down
    				--keystroke tab
    				key code 36
    				delay 8
    			end tell
    			
    			tell application "Safari"
    				do JavaScript "
    				try {
    					document.contactform[0].submit();
    					/*alert (\"Multiple Forms\")*/
    				}
    				catch(err)
    				{
    		    			document.contactform.submit();
    					/*alert (\"Single Form\")*/
    				};
    				" in document 1
    				
    			end tell
    			
    			delay 8
    			
    			set pageText to text of current tab of safariWindow
    			
    		end if
    	end repeat
    end tell
    I removed the tab after the Command-V. It is not needed. I also removed the delay between the tabs and the paste. That shouldn't be needed as well.

    I took out the "using option down" on the tabbing. Must be a setting in your browser for tabbing from field to field. Set as needed for you.

    The alerts() are commented out in the javascript. You can put them back in if you want to do a short run of tests to see how it is working. Just take them back out before you go into production since someone will have to be there to dismiss the dialogs or your processing will get messed up. Applescript won't know that the browser is waiting for a message to be dismissed.
     
  10. aapl-danial thread starter macrumors newbie

    Joined:
    Feb 8, 2013
    #10
    But I can brick it if an entry only has one form. Why would that be? For example: R000196

    That has one form, and the script says len-1, which is 1-1=0, should be right as any other.
     
  11. numero macrumors regular

    Joined:
    Jul 23, 2002
    Location:
    OR
    #11
    Mine is returning 10 on that page for some reason. That is why I did the work-around.
     

    Attached Files:

Share This Page