How to script link navigation
Utilities to check a site’s links can be used for repetitive maintenance or testing tasks. This can be scripted in Windows with only a few lines of code. Additionally, it can be expanded upon without any scripting or modification of the utility.
On a Windows machine, creating the link navigation script only requires a text editor. Two files are needed, one with a
.vbs extension, the other with a
.vbs file, we will first need to declare some variables:
Dim objIE Dim fSO, file, textStream Dim link
This will create an object which we can use to control Internet Explorer
(objIE) and some objects and variables for use with file Input/Output.
The second file (with the
.txt extension) is where we can put a list of links to have the utility navigate to. So, the script will need to know how to access this file. The
fSO is a filesystem object. It has many uses, but we can just use it to read from a list of links.
The file object can act as a handle to an actual file. This will be the file with the .txt file extension.
The textStream object provides access to useful methods when dealing with the text file, such as reading a whole line at a time.
The link variable can be used to store the actual link that we want to navigate to.
After declaring these variables, we need to instantiate the objects since they currently have no functionality. They are just arbitrary names and can be renamed to anything at this point:
Set objIE = CreateObject("InternetExplorer.Application") Set FSO = CreateObject("Scripting.FileSystemObject")
Set file = FSO.GetFile("LinksFile.txt")
Set textStream = FSO.OpenTextFile("LinksFile.txt", 1)
This will create the actual Internet Explorer object, then the filesystem object, then assign the file filehandle the text file containing the links. Then, the text file will be opened in reading mode, denoted by the 1 argument to the OpenTextFile method.
Now, the browser is accessible, as is the text file that would contain links.
Next, we need to set the browser to visible:
objIE.Visible = 1
At this point, we are ready to fetch links and navigate to each in a loop:
do While textStream.AtEndOfStream <> True link = textStream.ReadLine objIE.Navigate(link) Script.Sleep(4000) loop
This will create a loop that checks if the end of the file was reached, and will continue if it is not. VBScript do loops can run even if no While condition is stated. A runtime error will display however, when the end of the file is reached.
The loop is then finished, and will repeat as long as the while condition is true (or textStream.AtEndOfStream is False). When each link is navigated to, access to the file can be closed:
The script is configurable at this point, in terms of how long the browsing sleep times need to be, but no links have been specified.
In the .txt file, we will need to specify some links. In the file, all that is needed are locations you wish to navigate to, one on each line. You can specify as many or as few as you wish, and can change them as needed.
Running the utility can be performed from the command line:
Where ScriptName is the arbitrary name of the script file and the above cscript command is run from the directory where both the .vbs script and .txt file containing links are stored.