How do I fill in a certain form and sumbit on a webpage with automator (no keystrokes)

Hello!

I need to have automator fill in this web form and submit it (preferably with AppleScript). I had an original script that would fill in the form but not submit. I lost that script but I need AppleScript anyways. I have the pictures below of what the webpage source looks like. Thanks so much!

Posted on Nov 7, 2020 1:55 PM

Reply

Similar questions

7 replies

Nov 7, 2020 4:23 PM in response to finn108

What you ask for (specifically, 'no keystrokes') is technically impossible.


It is (or, at least, may be) possible to write a script to fill and submit the form, but something has to initiate that script. There is o way that Safari (or Chrome, et. al.) will automatically submit the form without some form of user action.


That said, the standard way of addressing this is actually via JavaScript, not AppleScript. JavaScript is the standard for interacting with web content. AppleScript can function at a higher level (manipulating windows, tabs, menus, etc.), but the nuances of HTML and all that goes into a web page is not its forte.


Now, AppleScript can be used to invoke a JavaScript action, so that's probably your way to go.


Given what you've posted, the following JavaScript should enter and submit the form:


document.getElementById('game-input').value="1234"
document.forms[0].submit()


Which can then be wrapped in an AppleScript do JavaScript command:


tell application "Safari"
  tell tab 1 of window 1
    do JavaScript "// your JavaScript goes here"
  end tell
end tell


Nov 12, 2020 2:17 PM in response to finn108

> I need to repeat this script so that it opens multiple windows and inputs the correct information on each tab.


I fear you're going to have to do a lot more work.


It's trivial to get AppleScript to repeat the actions for every tab in a window using a repeat loop


tell application "Safari"
	-- get all the tabs
	set allTabs to every tab of window 1
	-- iterate through them
	repeat with eachTab in allTabs
		tell eachTab
			-- your code here
		end tell
	end repeat
end tell


By extension, you can even iterate through every tab of every window:


tell application "Safari"
	--	get all the windows - use 'whose visible is true' since Safari maintains other hidden windows
	set allWindows to (get every window whose visible is true)
	-- iterate through the windows
	repeat with eachWindow in allWindows
		-- get all the tabs
		set allTabs to every tab of eachWindow
		-- iterate through them
		repeat with eachTab in allTabs
			tell eachTab
				-- your code here
			end tell
		end repeat
	end repeat
end tell


but I sincerely doubt that's what you want. Unless every tab/window has this form, the script is going to break. Therefore you need some additional logic to validate the tab is pointing to the web page you think it is/expect, or maybe a try block to catch errors such as missing form elements, etc..

Nov 24, 2020 8:53 PM in response to finn108

Hello again. Your script worked perfect. I just need it to work in chrome not safari. I would prefer a "do javascript" in apple script. This is what I have so far;


tell application "Chrome"

tell tab 1 of window 1

do JavaScript "// document.getElementById('game-input').value="1234"

document.forms[0].submit()"

end tell

end tell


It does not work and I'm not sure what to do. If its impossible to use applescript that's fine. Im just running it through automator so I can do a run javascript. Thanks so much!

Nov 25, 2020 2:27 PM in response to finn108

> It does not work and I'm not sure what to do


It doesn't work for two reasons.


One is that your JavaScript contains a single comment - the '//' is JavaScript's comment delimiter, so everything in that line is going to get ignored.

In addition to that, if you're trying to include a quote symbol (") in your script you need to escape it, otherwise AppleScript will interpret it as the end of the string you're trying to pass. I only used it in my example to denote 'your JavaScript goes here' as a comment.


Secondly, each application is different. Chrome does not have a 'do JavaScript' command. Instead it has a corresponding 'execute' command. This is an example of having to understand the semantics of the application you're trying to drive.


The equivalent command for Chrome would be:


tell application "Google Chrome"
    execute front window's active tab javascript "document.getElementById('game-input').value=\"1234\"; document.forms[0].submit()"
end tell


> Im just running it through automator so I can do a run javascript.


That's a lot more work. Automator's run JavaScript action will execute the javascript code within the Automator environment. You have to add all the overhead of attaching to an application process and targeting its environment. Not impossible, but while filling a form via JavaScript is pretty easy, full app automation is a whole different thing, especially since if you are well versed in JavaScript you'd likely already know the form-filling actions. Good Luck :)

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How do I fill in a certain form and sumbit on a webpage with automator (no keystrokes)

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.