Home » Blog
date 1.Mar.2015

■ Internet Explorer browser automation for dummies

Platitude alert: nowadays you can do anything with a browser. Think of crack tracker, it searches for keywords in warez search engines, and reads back results. This is achieved by embedding the webbrowser control (essentially internet explorer) and scripting it to do automated tasks like filling forms, reading data, turning pages and so on. There are all sorts of similar needs for businesses generally known as data extraction or screen scraping.

Crack tracker uses its own specialized scripting engine and language. There are alternative scripting frameworks like iMacros and Twebst you can use for low level scripting, offering minute control. But if you don't really care about being too efficient (things like turning off scripting, not loading images etc), you can go a long way just with internet explorer's built-in automation and a scripting language like VBS, e.g. through windows scripting host (see here for a series of articles on WSH)

Here's a simple automation task: browse google website, insert a search term then go for it. Each item you see on google's page has a name for identification. You can see all the DOM details if you View source or press <F12> key to start the developer tools in your browser. The input box is an HTML element called "q":

<input name="q" title="Search" ... value=""/>

Here's a first very simple way to find and fill out google's keyword input element. It creates a new IE window, browses to google then fills out the search term:

google website from the inside
set wbr = wscript.CreateObject("InternetExplorer.Application")
wbr.Visible = true

' Give it some time to load the page and then get the document.

set edit = wbr.Document.getElementsByName("q").item(0)
edit.value = "test"

As with all scripting we read the fine manual to find methods and properties that do the job we need. You will notice that we have a little trouble waiting for IE to load google's webpage. We have to wait for the page to load fully before we can fill in the search form. The sleep time of 3 seconds (=3000 ms) is arbitrary. A better way is to install an event listener and let IE tell us when the page has actually finished loading. Here's draft #2:

' scripting doesn't support early binding
' Dim WithEvents ie As InternetExplorer.Application
' 2nd argument is function prefix for events
set wbr = wscript.CreateObject("InternetExplorer.Application", "objIE_")

' Make it visible and navigate to a given URL.
wbr.Visible = true

' wait for document complete event
waitDC = 1
Do While waitDC = 1
   WScript.Sleep 500

' Fill out search field
set doc = wbr.Document
set edit = doc.getElementsByName("q").item(0)
edit.value = "test"

' submit the form
set submit = doc.getElementsByName("f").item(0)

Sub objIE_DocumentComplete(byval pdisp, url)
waitDC = 0
End Sub
' -----

Notice how we used the second argument to CreateObject to hook up to the event loop. Save this simple script as AUTOGOOGLE.VBS then run it to see it work. And there you have it, a poor man's crack tracker in the making <g>

Post a comment on this topic »

Share |

©2010-2015 ZABKAT, all rights reserved | Privacy policy | Contact email