COM Google search example

Post your working scripts, libraries and tools for AHK v1.1 and older
ameyrick
Posts: 122
Joined: 20 Apr 2014, 18:12

COM Google search example

05 Mar 2015, 19:41

Hi all,
I just wanted to share my first COM script after reading up on an awesome tutorial by Mickers
"Basic Ahk v1.1+ COM Tutorial for Webpages": - http://www.autohotkey.com/board/topic/6 ... -webpages/

MyScript is an example of browser automation using COM to navigate HTML DOM
This very basic example loads Google into a Gui window & then automates a search query.
Pointless I know but was just to prove concept.

Code: Select all

; COM_Google_Search.ahk
; by ameyrick.
;
	google_query_string := "Site: www.w3schools.com JavaScript HTML DOM"
;
; A very basic example of browser automation using COM to navigate HTML DOM
; This example loads google into a Gui window & automates a search query.
;
; This is my first COM script after reading up on an awsome tutorial by Mickers
;	 "Basic Ahk v1.1+ COM Tutorial for Webpages"
;    http://www.autohotkey.com/board/topic/64563-basic-ahk-v11-com-tutorial-for-webpages/
;
; DOM Documentation:
; 	http://www.w3schools.com/jsref/dom_obj_all.asp

#NoEnv
#SingleInstance Force
SetBatchLines -1
ListLines Off
ComObjError(False)


; =================================
;		GUI
; =================================
Gui, Margin, 0, 0
Gui Add, ActiveX, w800 h600 vWB, Shell.Explorer  ; web browser
ComObjConnect(WB, WB_events)  ; Connect WB's events to the WB_events class object.
Gui, Show, w800 h600

; =================================
;		Open url, and wait for page to finish loading
; =================================
OpenLoginPage:
WB.Navigate("http://www.google.co.uk")
While WB.readystate != 4 or WB.busy
	Sleep 10
	
doc := WB.doc					
sleep, 500

; =========================================
; 		search all "input" <tags> for the search bar & enter the query string
; =========================================
q := doc.getElementsByTagName("input")					;// get a list of all input tags
loop, % q.Length										;// loop through list of input tags
{
	if ( q[(A_index-1)].title == "Search"){				;// check the "title" attribute of the input tag
		q[(A_index-1)].Value := google_query_string 	;// enter the query string
		sleep, 100
		break
	}
}

; ==========================================
;      Submit the <form id="tsf"> to process our query
; ==========================================

doc.getElementByID("tsf").Submit()						;//	Submit form "tsf"
return

Last edited by ameyrick on 15 Apr 2015, 17:03, edited 1 time in total.
User avatar
Soft
Posts: 174
Joined: 07 Jan 2015, 13:18
Location: Seoul
Contact:

Re: COM Google search example

06 Mar 2015, 10:37

neat!
AutoHotkey & AutoHotkey_H v1.1.22.07
User avatar
Iomega0318
Posts: 83
Joined: 06 Apr 2015, 14:56
Location: Wolfforth, TX
Contact:

Re: COM Google search example

14 Apr 2015, 09:00

I just came across this and I hope this isn't too old to reply to, but it does not seem to work anymore. I think the way it appears is pretty neat though :)
ameyrick
Posts: 122
Joined: 20 Apr 2014, 18:12

Re: COM Google search example

15 Apr 2015, 17:05

Iomega0318 wrote:I just came across this and I hope this isn't too old to reply to, but it does not seem to work anymore. I think the way it appears is pretty neat though :)
Thanks,
To confirm there seems to be an intermittent issue with the Click() event.
I've updated the above script to use the form Submit() instead.
So far Submit() appears to be more reliable.

Very strange as Click() works fine on other sites.
User avatar
Iomega0318
Posts: 83
Joined: 06 Apr 2015, 14:56
Location: Wolfforth, TX
Contact:

Re: COM Google search example

15 Apr 2015, 21:49

Could just be Google..
Thank you, I will give it a run tomorrow when I am on my laptop and let you know how it runs :)
User avatar
Iomega0318
Posts: 83
Joined: 06 Apr 2015, 14:56
Location: Wolfforth, TX
Contact:

Re: COM Google search example

28 Apr 2015, 14:32

So I don't know why but this thing does not want to work for me lol, it will load the page but it will not look for the search box or type anything in, and it still doesn't click anything.. I should mention I am still using IE 8 since my work actually requires this version, maybe that's why? :|

**EDIT**
I got it to type in the search by changing "Search" to "Google Search", but I can't get it to submit it..
ameyrick
Posts: 122
Joined: 20 Apr 2014, 18:12

Re: COM Google search example

28 Apr 2015, 17:23

I'm using IE 11 although I think it's probably to do with your IE security settings for ActiveX controls

IE > Internet Options > Security > Internet > Custom Level > Check your ActiveX security Settings.
User avatar
Iomega0318
Posts: 83
Joined: 06 Apr 2015, 14:56
Location: Wolfforth, TX
Contact:

Re: COM Google search example

29 Apr 2015, 08:44

I tried enabling them all but still no go, I will play around with it some more.. what's weird is if I change the script slightly to where it clicks a button on another site it'll work fine but I can't get it to work with Google which is what bugs me lol..

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 149 guests