Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get current title from IE object 3

Status
Not open for further replies.

JPJeffery

Technical User
May 26, 2006
600
GB
I'm writing a script which, among other things, has to check for internet connectivity.

My idea is to create an IE object, browse to a website (a high visibility and high reliability one such as and test to see if that has worked by fetching the current browser object's window title (I was hoping to check for a returned error number or an event in the local event log but couldn't find either).

If it hasn't worked then I'd expect the window title to include the text "cannot display the web page".

I can't see how to get that title though. I've tried this to get what I'm after
Code:
MsgBox objIE.LocationName & vbCrLf & objIE.LocationURL
but that just returns blanks.

Here's my function code:
Code:
'-----------------------------------------
Sub CheckInternet()
    Set objIE = wscript.CreateObject("InternetExplorer.Application","objIE_")
    If Err.Number <> 0 Then
        On Error GoTo 0
        Msg = "IE application not found."
        MsgBox Msg,vbExclamation,strAppDescription
        Wscript.Quit
    End If
    On Error GoTo 0

    objIE.visible = true
    objIE.ToolBar = 0
    objIE.statusbar=false
    objIE.Left = 0
    objIE.Top = 0
    objIE.Width = 1024
    objIE.Height = 768
    objIE.Navigate "[URL unfurl="true"]http://www.microsoftbubububububububububbubuubuu.com"[/URL]
    wscript.echo "Err.Number = '" & Err.Number & "'"
    MsgBox objIE.LocationName &_
           vbCrLf & objIE.LocationURL
           
    do while not bQuit
        wscript.sleep 500
    loop
End Sub
'-----------------------------------------
Sub objIE_OnQuit
'wscript.echo "objIE_quit has been called"
    bQuit=true
End Sub

All suggestions (on how to get what I'm after or alternative approaches) gratefully received.

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
You may add the following just after the Navigate call:
Do While objIE.Busy = True
WScript.Sleep 500
Loop

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
OK, that makes the
Code:
MsgBox objIE.LocationName & vbCrLf & objIE.LocationURL
work, which is A Good Thing(tm) - unfortunately they don't result in the title text I'm after to prove that the browse didn't work...

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
[tt] msgbox objIE.document.title[/tt]
Whether it is a good idea to detect title as a link checker, I can only say it would not be a terribly efficient solution. But for lighter job, it should have its place.
 
Thanks, tsuji

Well, that was all I could think of. If there's a better way you know of...

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
You could look at the HTML body for a specific error too...just an idea.

Code:
	If InStr(objIE.Document.Body.innerHTML, "Internet Explorer cannot display the webpage") Then
		WScript.Echo "Bad site!"
	Else
		WScript.Echo "Good site!"
	End If

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Sorry...let me correct.

Code:
	If InStr(objIE.Document.Body.innerHTML, "Internet Explorer cannot display the webpage") > 0 Then
		WScript.Echo "Bad site!"
	Else
		WScript.Echo "Good site!"
	End If

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Nice one, thanks.

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
A blast from the past.

Tests I perfomed back in June showed that this Internet connectivity test was working as desired.

Now however, subsequent tests (when changing how my script reports errors) showed that the function always returns an "OK" result even if I get it to test a known-to-be-bad URL. e.g.
Code:
Tested internet connectivity to [URL unfurl="true"]http://www.microsoftyness.com[/URL] OK

So, WTF is going on?

Here's the current version of the function (with the strTestURL variable initially set to "
Code:
Sub CheckInternet(strTestURL)
    wscript.echo "CheckInternet called"
    Set objIE = wscript.CreateObject("InternetExplorer.Application","objIE_")
    If Err.Number <> 0 Then
        On Error GoTo 0
        Msg = "IE application not found."
        MsgBox Msg,vbExclamation,strAppDescription
        Wscript.Quit
    End If
    On Error GoTo 0

    objIE.visible = false 'true
    objIE.ToolBar = 0
    objIE.statusbar=false
    objIE.Navigate "about:blank"
    Do Until objIE.ReadyState = 4
        wscript.echo "ReadyState Not 4"
        WScript.Sleep 100
    Loop
    objIE.Navigate strTestURL
    Do While objIE.Busy = True
        wscript.echo "objIEBusy=True"
        WScript.Sleep 500
    Loop
    
    ' [URL unfurl="true"]http://www.tek-tips.com/viewthread.cfm?qid=1383115&page=1[/URL] refers to
    ' following loops
    'WScript.Sleep 500
    do while objIE.Document is nothing
        wscript.sleep 50
        wscript.echo "Document is nothing"
    loop
    do while objIE.Document.body is nothing
        wscript.sleep 50
        wscript.echo "Document body is nothing"
    loop
    wscript.echo "START If InStr(objIE.Document...> 0 Then TEST"
    dim intTintResult
    intTintResult = objIE.Document.Title
    wscript.echo "--intTintResult = " & intTintResult
'    If InStr(objIE.Document.Body.innerHTML, "Internet Explorer cannot display the webpage") > 0 Then
    If InStr(objIE.Document.Title, "Internet Explorer cannot display the webpage") > 0 Then
        strTempErrorMsg = "  Unable to connect to " & strTestURL & ". Trying "
        strInternetErrorText = strInternetErrorText & strHTMLTab & strHTMLTab & "Unable to connect to " & strTestURL & ".<br>"' & ". Trying " &
        strInternetError=True
    Else
        strInternetError=False
        wscript.echo "  Tested internet connectivity to " & strTestURL & " OK."
        strReport = strReport &_
                    strHTMLTab & "OK<br>"
    End If
    objIE.quit

    if strInternetError=True then
        if bSecondAttempt=True then
            wscript.echo "  Also unable to connect to " & strTestURL & ". Testing infrastructure servers..."
            CheckDNSAndGateway()
            exit sub
        Else
            bSecondAttempt=True
            bInternetError=True
            strTestURL = "[URL unfurl="true"]http://www.directlyness.gov.uk"[/URL]
            strTempErrorMsg = strTempErrorMsg & strTestURL & ". Please wait."
            wscript.echo strTempErrorMsg
            CheckInternet(strtestURL)
        End If
    end If
End Sub

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
Rather than have you Sub check the various URL's I'd change it to a function and have it check one address and return True/False if the site is accessible. Do the additional checking outside of the Sub.

i.e.

Code:
Option Explicit

Main()

Sub Main()
	Dim strURL1 : strURL1 = "[URL unfurl="true"]http://www.microsoftyness.com/"[/URL]
	Dim strURL2 : strURL2 = "[URL unfurl="true"]http://www.google.com"[/URL]
	
	If IsURLValid(strURL1) Then
		WScript.Echo strURL1 & " is accessible.  . Internet access is OK."
	Else
		WScript.Echo strURL1 & " is NOT accessible. Will check the second URL."
		If IsURLValid(strURL2) Then
			WScript.Echo strURL2 & " is accessible. Internet access is OK."
		Else
			WScript.Echo strURL2 & " is NOT accessible. Failed to verify internet connectivity."
		End If
	End If
End Sub

Function IsURLValid(strURL)
	IsURLValid = False
	On Error Resume Next
	Dim objIE : Set objIE = CreateObject("InternetExplorer.Application")
	If Err.Number <> 0 Then
		MsgBox "Could not create IE object!", vbCritical + vbOKOnly, "IE object error"
		WScript.Quit		
	End If
	On Error GoTo 0
	
	objIE.visible = False
	objIE.ToolBar = 0
	objIE.statusbar = False
	objIE.Navigate "about:blank"
	
	Do Until objIE.ReadyState = 4
		WScript.Sleep 500
	Loop
	
	objIE.Navigate strURL
	
	Do While objIE.Busy
		WScript.Sleep 500
	Loop
	
	Do While objIE.Document Is Nothing
		wscript.sleep 500
	Loop
	
	Do While objIE.Document.body Is Nothing
		wscript.sleep 500
	Loop
	
	Dim strTitle : strTitle = UCase(objIE.Document.Title)
	If InStr(strTitle, "INTERNET EXPLORER CANNOT DISPLAY THE WEBPAGE") = 0 Then IsURLValid = True
	
	objIE.quit
End Function

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Thanks, DM.

Turns out my code does work, just not on my own PC! FFS!

Anyway, I like your approach too, and I didn't know you could declare a variable and populate with a value on the same line with the : character. All good.

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top