IT Nerd Space

Script to open URLs and take screenshots

Script to open URLs and take screenshots

Script to open URLs and take screenshots

Today I wanted to visually check a lot of URLs, to see if the page was loading fine, or if it was giving any kind of error. So I had an Excel file with the name of some applications (Azure WebApps), and for each the list URLs of that site, and I needed to load each URL and see, in a browser, what the pages look like.

The good thing is that depending the result, I could easily identify it if I had a high level view of all the pages. That’s were the thumbnail view comes in!

To do that I needed to automate several individual steps that I would later combine.

So I created a Frankenstein Powershell script combining all the pieces together, which you can see here.

The input I’m using is an Excel file, of which I’ll use two columns: Name and Hostnames, which contains a list of comma-separated URLs, of which I’ll only take the first one. I’ll use the Name column to name the output screenshot file.

This is how the Excel file would look like:

We load it using Import-Excel CmdLet. In the example case above, we would get:

PS C:\> Import-Excel D:\temp\file.xlsx

Name HostNames
---- ---------

I didn’t find a way to take a screenshot of a non visible window, so I am showing the browser, and taking the screenshot of the region. For my purpose it works and it’s quite simple, but that presents some disadvantages (you cannot use the region of the screen while running the script, or you risk altering the result in the screenshot).

Another disadvantage is that it involves some precaution and some manual preparation, to position the windows, and modify the script accordingly, the first time at least.

  • Open a Powershell command line window
  • Run the following command. This will open an Internet Explorer window. Place it on your desktop, so that it will not overlap with the Powershell window. They can be side by side. It will be easier if you have two monitors, as you can place the browser window alone on a monitor.
 $IE=new-object -com internetexplorer.application
  • Run the following commands and take note of the results:
  • Replace the results in the script. That way, each time we open a new browser the script will reposition it in the same location on your screen!

That’s it. Now you just have to run the script and it will do the job.

In the case of our example input file above, the output we’d get would look like in the output folder places in Thumbnail View:

In my particular use case, this below is what I was looking for:

As you can see, by placing the images in Thumbnail View, we can rapidly identify and classify the corresponding Web sites into 4 groups:

  1. Page seems to load fine
  2. Default initial Azure page (possibly no content deployed)
  3. Page doesn’t load, or takes too long
  4. Some HTTP error

Furthermore, within the first category, a quick inspection of the image will show if the page loads apparently fine, or if it shows some content problem.

If you have hundreds of sites, it can save you some precious time!

Some considerations:

  • I’m not sure what happens if the screen switch to screensaver mode. It may not work as expected. So, either deactivate the screensaver, or keep moving the mouse (out of the browser’s way) while the script runs.
  • I use a new IE instance for each new URL, that is, I don’t recycle the IE instance for several URLs, for a simple reason: if the URL doesn’t load, I would possibly take a screenshot of the previously loaded URL, which is not what I want.
  • VACoder

    This is cool. Could you provide a little more detail on how you structured your excel file?

    • I forgot to detail that part. I have edited the post above with more detail about the Excel file format. Truth is that the Import-Excel powershell module is awesome and makes it really easy to input/ouput data from/to Excel format. I use it a lot. I hope that answers your question 🙂