Automate filling webforms

The problem:

There is a web form license generator for an attendance system. One part of license depends on the day when it is generated. I needed to observe the license pattern for each day. There was auto fill option in the web browser, however, every time I need to open the link, and select auto fill and click on send mail. Everyday I had to do that! Autofill reduced my work a bit, but still there’s some manual work. This post is about automating it!

The solution:

AutoHotkey helps automate things! It solved my major problem. A forum member (AutoHotKey) helped me out with the coding. Here’s how it works, when the .ahk is run, upon pressing a key (defined in the code). Internet explorer opens, form is filled and it automatically clicks on send mail button and internet explorer closes in 5 seconds. I would receive a mail with the license key. The .ahk script will NOT be closed.

That was good! Still I have to press a key to run it. A line of VBA script solved the problem, whenever the VBA script is executed, it presses the key.

Windows scheduler solved the problem of executing the VBA script, I created a task to run every 24 hours which executes the above VBA script.

Now, everyday at 10:30 (Indian Standard Time), the task runs which presses the F11 key (mentioned in VBA script) and .ahk script is executed and I receive mail containing the license. 😊

Usually, there will be a ahk file on the system tray icon. When someone accidentally closes it, the whole setup stops working. To overcome that issue, I added #NoTrayIcon line in the ahk file, which hides it from the system tray.

AHK script

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#NoTrayIcon
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.


F11::
Data:= { "ctl00$MainContent$txt_company":"Val Technologies", "ctl00$MainContent$txt_Location":"Dubai", "ctl00$MainContent$txt_ActivationCode":"00111726908643","ctl00$MainContent$email":"hamid@grr.la","ctl00$MainContent$phone":"22254123"}
url:="http://www.so365.in/elicense/elicense.aspx" ;example form
urltab:="so365.in"


wb := ComObjCreate("InternetExplorer.Application")
wb.Visible := true

wb.Navigate(url)

while wb.readyState!=4 || wb.document.readyState != "complete" || wb.busy
sleep 10

for Field, Value in Data
{
wb.document.all[Field].value := Value
Sleep, 100
}

;wb.document.getElementByID("ctl00$MainContent$btn_SendMail").Click()
Loop % (input := wb.document.getElementsByTagName("input")).length ;-->loop through all input tags and click on any that contain the specifie value
if (InStr(input[A_Index-1].value,"send")){
input[A_Index-1].click()
break
}

sleep 5000
SoundBeep
;wb.quit
;wb:=""
;^The Above should work, but i find this is more reliable....
HWND := wb.hwnd
WinGet PID, PID, ahk_id %HWND%
Process, Close, % PID
return

 

VBA Script

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys "{F11}"

Hope you found this article useful, maybe it can help automate any one of your tasks. Thanks for reading!

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s