Monday, May 20, 2019

#1 2019-05-10 01:12:07 am

hansolo625
Member
Registered: 2019-04-17
Posts: 14

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 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

Applescript:


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

Offline

 

#2 2019-05-10 07:24:25 am

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 392

Re: Using AppleScript to fill webforms

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.


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

#3 2019-05-10 08:44:40 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 123

Re: Using AppleScript to fill webforms

hansolo625 wrote:

• 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.



Just the opposite. This is not a site issue, but a minimal protection of the site against input errors. The "Search" button becomes active (that is, accessible) when all the required fields are filled. With manual input, the user clicks the form fields to fill them. Thus, these fields receive focus (that is, the field is activated). When you fill in the fields using AppleScript, the mouse remains elsewhere on the screen and the fields are not focused.

The conclusion is this: you need with your AppleScript to move the mouse pointer to the last filled field of the form, simulate a click on the form using System Events, hold the necessary pause and click there again. Then move the mouse pointer with your AppleScript to the "Search" button and pull out the desired pause. Then this button should be activated.

That is, in order for the webpage's method to recognize your actions using a script, everything (or almost all) should be as if you were entering data manually... "Safari isn't registering that the fields have content filled in it and keeps the "Search" button greyed out." Exactly, this does the webpage's method.

Last edited by KniazidisR (2019-05-10 09:22:36 am)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Online

 

#4 2019-05-10 09:51:31 am

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 392

Re: Using AppleScript to fill webforms

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;


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

#5 2019-05-13 02:46:10 am

hansolo625
Member
Registered: 2019-04-17
Posts: 14

Re: Using AppleScript to fill webforms

KniazidisR wrote:

The conclusion is this: you need with your AppleScript to move the mouse pointer to the last filled field of the form, simulate a click on the form using System Events


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!

Last edited by hansolo625 (2019-05-13 02:56:48 am)

Offline

 

#6 2019-05-13 02:56:34 am

hansolo625
Member
Registered: 2019-04-17
Posts: 14

Re: Using AppleScript to fill webforms

t.spoon wrote:

Something like:

document.exampleform.submit();

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



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)

Applescript:

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

t.spoon wrote:

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.


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! 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!!

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)