Using AppleScript to fill webforms

Hi scripting experts! I’m at a roadblock with the new tool I’m scripting and I’d love to consult the experts here :slight_smile:

I’m using AppleScript to pre-fill information on a webform to perform a search. Below are the two JavaScript handlers that I’ve obtained from other forum


--Insert by element ID
to inputByID(theId, theValue)
	tell application "Safari" to do JavaScript "document.getElementById('" & theId & "').value ='" & theValue & "';" in document 1
end inputByID

--Insert by element Class
to inputByClass(theclass, num, theValue)
	tell application "Safari" to do JavaScript "document.getElementsByClassName('" & theclass & "')[" & num & "].value ='" & theValue & "';" in document 1
end inputByClass

• Goal:
I’m doing searches on a webform and need to run one search criteria each time. Rather than do one search, refresh, another; I would open multiple tabs and do one criteria search on each tab. This is where AppleScript will come in handy!

By using one of the handlers above I’ve managed to tell Safari to open multiple tabs and pre-fill the information I need at the correct fields.

• Roadblock:
Safari isn’t registering that the fields have content filled in it and keeps the “Search” button greyed out. Usually when you key in something, even a space, the Search button would be enabled and you can click on it. It appears that when texts are pre-filled with AppleScript, Safari is thinking that there is nothing in the fields.

Is that an issue with the website or with the script using JavaScript, and is it possible to get around it? I thought of using System Events to automate the typing part but I’m unsure if that’s feasible.

Thanks x1000!!!

NOTE: Unfortunately the webform I’m working on is a private site that cannot be shared publicly.

I don’t know enough about Javascript and HTML to know if the ability to submit the form is actually disabled when the “Search” button is greyed out, but you might try simply submitting the form via Javascript. Something like:

document.exampleform.submit();

but adjusted for the actual form name in the relevant site.

If that doesn’t work - it’s a less robust and elegant solution, but as long as the page in questions stays relatively static, it is likely you could use UI scripting to control the keyboard to tab through the form and type in the information.

It’s hard for us to provide specific help without access to the page in question.

Come to think of it, if sending a javascript submit to the form doesn’t work when button is greyed out, then maybe you can use javascript to first enable the button then use javascript to submit th form, without resorting to UI scripting.

Again, this would need adapting to your specific page.

document.getElementById(“Button”).disabled = false;

That’s precisely it! But when I looked up how to simulate a mouse click with AppleScript, it looks like you’ll need to give it coordinates, which is not really desirable as my browser is never at the same location lol

Rather than simulating clicks, I think t.spoon’s suggestion of controlling the keyboard to tab through the form is going to be the solution. I’ve tentatively overcome this by having System Event tab the correct times and Keystroke the input and return

Not elegant or robust but… gets the job done lol :lol:

Thank you SO much!

I got this to work with the following script, but the only thing is that all it does is refresh the page lol (I’m not entirely sure if I found the right name to replace “exampleform” but that’s the only replacement that did something so I assume it was correct)

tell application "Safari" to do JavaScript "document.myform.submit()" in document 1 

This is my current solution :lol: I solved the roadblock by opening a new tab, tab the correct times and keystroke the input and then return. As you said, not as elegant and robust but hey, gets the job done! :stuck_out_tongue:

I’m very curious about using

document.getElementById(“Button”).disabled = false;

I might have to give that more try when I have time!

Thanks so much!!