Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Filling web forms using COM


  • Please log in to reply
11 replies to this topic
m3user
  • Members
  • 33 posts
  • Last active: Jun 26 2014 08:50 AM
  • Joined: 29 Mar 2014

Hi, I thought I would create a script which would automatically fill-in some standard fields on any form on the net by searching for the field text on the page (using COM), then moving to the right to entry field (with sending a Tab) and finally sending a field value.

 

Unfortunately my example script below only fills some fields but not all so obviously I'm doing something wrong.

 

Any ideas? Alternatively, could something like this be done without sending keys and only using COM? Thanks!

return

F10::
Data:= { "first name":"John", "surname":"Smith", "number":"6", "street":"MyLongStreet", "town":"MyLittleTown", "city":"MyBigCity", "zip":"1234"}
url:="https://www.onespaworld.com/main/Applicationform.aspx"  ;example form
urltab:="OneSpaWorld.com"

WinActivate ahk_class IEFrame
pwb := IEGet(urltab)
While pwb.busy
    Sleep, 50

range := pwb.document.body.createTextRange()
for Field, Value in Data
{
    range.findText(Field)
    range.select()
    Sleep, 1000
    Send, {Tab}%Value%
    Sleep, 1000
}
return


IEGet(Name="")        ;Retrieve pointer to existing IE window/tab
{
    IfEqual, Name,, WinGetTitle, Name, ahk_class IEFrame
        Name := ( Name="New Tab - Windows Internet Explorer" ) ? "about:Tabs"
        : RegExReplace( Name, " - (Windows|Microsoft) Internet Explorer" )
    For wb in ComObjCreate( "Shell.Application" ).Windows
        If ( wb.LocationName = Name ) && InStr( wb.FullName, "iexplore.exe" )
            Return wb
} ;written by Jethrow


m3user
  • Members
  • 33 posts
  • Last active: Jun 26 2014 08:50 AM
  • Joined: 29 Mar 2014

Any ideas?

 

I forgot to mention that you need to first open https://www.onespawo...cationform.aspx in Internet Explorer (example form).

Thanks!



Jackie Sztuk _Blackholyman
  • Spam Officer
  • 3757 posts
  • Last active: Apr 03 2016 08:47 PM
  • Joined: 28 Feb 2012
I'll have a look and give you some idea's later if no one else does first :)
Helping%20you%20learn%20autohotkey.jpg?d

[AHK] Version. 1.1+ [CLOUD] DropBox ; Copy [WEBSITE] Blog ; About

m3user
  • Members
  • 33 posts
  • Last active: Jun 26 2014 08:50 AM
  • Joined: 29 Mar 2014
Thanks Blackholyman, any idea is welcome.

Jackie Sztuk _Blackholyman
  • Spam Officer
  • 3757 posts
  • Last active: Apr 03 2016 08:47 PM
  • Joined: 28 Feb 2012

here is something

 

F10::
Data:= { "FirstName":"John", "LastName":"Smith", "StreetNumber":"6", "StreetName":"MyLongStreet", "TownName":"MyLittleTown", "City":"MyBigCity", "PostCode":"1234"}
url:="https://www.onespaworld.com/main/Applicationform.aspx"  ;example form
urltab:="OneSpaWorld.com"
 
 
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
}
return

 

Not sure if thats what you need, but that works for your example :)

 

Hope to help


Helping%20you%20learn%20autohotkey.jpg?d

[AHK] Version. 1.1+ [CLOUD] DropBox ; Copy [WEBSITE] Blog ; About

m3user
  • Members
  • 33 posts
  • Last active: Jun 26 2014 08:50 AM
  • Joined: 29 Mar 2014

Thanks, this is great and exactly what I wanted. You are very kind!

 

May I have few more questions?

- where can I find more documentation about

wb.document.all[Field].value := Value

I would like to understand what and how exactly it does.

 

- my idea was to press F10 on the page which is already opened. When I tried to combine your script with mine it failed. The script below does nothing. Any idea what's the problem?

F10::
Data:= { "first name":"John", "surname":"Smith", "number":"6", "street":"MyLongStreet", "town":"MyLittleTown", "city":"MyBigCity", "zip":"1234"}
url:="https://www.onespaworld.com/main/Applicationform.aspx"
urltab:="OneSpaWorld.com"

WinActivate ahk_class IEFrame
pwb := IEGet(urltab)
While pwb.busy
    Sleep, 50

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


IEGet(Name="")        ;Retrieve pointer to existing IE window/tab
{
    IfEqual, Name,, WinGetTitle, Name, ahk_class IEFrame
        Name := ( Name="New Tab - Windows Internet Explorer" ) ? "about:Tabs"
        : RegExReplace( Name, " - (Windows|Microsoft) Internet Explorer" )
    For wb in ComObjCreate( "Shell.Application" ).Windows
        If ( wb.LocationName = Name ) && InStr( wb.FullName, "iexplore.exe" )
            Return wb
} ;written by Jethrow

 



Jackie Sztuk _Blackholyman
  • Spam Officer
  • 3757 posts
  • Last active: Apr 03 2016 08:47 PM
  • Joined: 28 Feb 2012
http://www.autohotke...t-com-tutorial/

The field value's of your array does not Match the DOM element names from you webpage.

Try Reading the tutorial i linked to and see if that helps

Else your welcome to ask again :)
Helping%20you%20learn%20autohotkey.jpg?d

[AHK] Version. 1.1+ [CLOUD] DropBox ; Copy [WEBSITE] Blog ; About

m3user
  • Members
  • 33 posts
  • Last active: Jun 26 2014 08:50 AM
  • Joined: 29 Mar 2014
Thanks, Thanks for the link, clear now. I see now that you changed the element names in the array. Probably you looked into to webpage with some software and recognized the element names.

Actually, my idea was to have an universal script which could be used on any form. For example, on every form there would be some standard elements to be filled-in like name, surname, adress etc. The script would look for all controls on the page and if it would find something which would include such text, it would will it in automatically. Any idea how to go through all controls on the page and read their names, IDs etc?

As element names could not use meaningful text, My first idea (first script) was to look for the text on the page, move right with a Tab and fill it with value. Unfortunately it doesn't work properly but I think it is close.

Jackie Sztuk _Blackholyman
  • Spam Officer
  • 3757 posts
  • Last active: Apr 03 2016 08:47 PM
  • Joined: 28 Feb 2012
Yea its not a bad idea in its own way but one of the things i Can see being an issue is that the findText method does not return a Html element you Can use...

This Can lead to other problems like script instability...

There is something that just popped up as an idea but let me try it out first then i'll be back with my findings later...

Hope to help
Helping%20you%20learn%20autohotkey.jpg?d

[AHK] Version. 1.1+ [CLOUD] DropBox ; Copy [WEBSITE] Blog ; About

Jackie Sztuk _Blackholyman
  • Spam Officer
  • 3757 posts
  • Last active: Apr 03 2016 08:47 PM
  • Joined: 28 Feb 2012

Here i made a script that looks at all the attributes of all the input elements of a page and trys to match them with the strings from the types array in my data object
 
Its late here no comments in the script... but feel free to ask
 

F10::
Data:= { "FirstName":{"value":"John"
			,"types":["FirstName", "First name", "User Name"]}
		, "LastName":{"value":"Smith"
			,"types":["LastName", "surname", "Last name", "family name"]}
		, "StreetNumber":{"value":"6"
			,"types":["number", "StreetNumber", "Street Number"]}
		, "StreetName":{"value":"MyLongStreet"
			,"types":["street", "StreetName", "Street Name"]}
		, "TownName":{"value":"MyLittleTown"
			,"types":["Town", "TownName", "Town Name", "Home Town"]}
		, "City"	:{"value":"MyBigCity"
			,"types":["City", "CityName", "City Name"]}
		, "PostCode":{"value":"1234"
			,"types":["PostCode", "Post Code"]}}
		
url:="https://www.onespaworld.com/main/Applicationform.aspx"  ;example form
urltab:="OneSpaWorld.com"


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

wb.Navigate(url)

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


All_inputs := wb.document.getElementsByTagName("input")


for Field, items in Data
{
	ToolTip % "Field " A_index " of " (Data.GetCapacity()-1)
	if IsObject(items)
	{
		for key, val in items.types
		{
			loop % All_inputs.length
			{
				MouseGetPos, x, y
				ToolTip, % "looking over " A_index " out of " All_inputs.length " Elements", % (x+18), % (y+40), 2
				elem := All_inputs[A_index-1]
				if (elem.value != "")
					continue
				loop % (Attribs := Elem.attributes).length
				{
					attrib := Attribs[a_index-1]
					if (attrib.specified) 
						if (attrib.value != "")
						{
							if (val ~= attrib.value)
							{
								Elem.value := items.value
								break 3
							}
						}
				}
			}
		}
	}
	ToolTip,,,, 1
	ToolTip,,,, 2
}	
msgbox done
return

Its not that fast now but it just needs a little more work...

I can't say if this will work for a lot of sites but it may.


Helping%20you%20learn%20autohotkey.jpg?d

[AHK] Version. 1.1+ [CLOUD] DropBox ; Copy [WEBSITE] Blog ; About

Guest10
  • Members
  • 1216 posts
  • Last active: Oct 30 2015 05:12 PM
  • Joined: 27 Oct 2012

am i correct to presume that there are no 100% foolproof ways to fill out forms online automatically such as there are no guaranteed machine-translation? is the above code an attempt to fill "any" online form automatically? is so, it would be fantastic if the final script is posted with some commentary (comments for uninitiated masses). ;)



m3user
  • Members
  • 33 posts
  • Last active: Jun 26 2014 08:50 AM
  • Joined: 29 Mar 2014
Thank you very much Blackholyman for your script, it works well. I will only be able to make more detailed tests next week (leaving my computer now) when I may have some additional questions. :)

And thanks again for showing great examples about browsing through the page elements etc.