Automation of IE using VBscript 0x80010108 - unknown exception

1

I'm trying to click on a button on a webpage using VBScript. The rest of my script works just fine. But at different instances of using the browser object I get an exception. The same code works on another script for a different website.

Here's my code:

Set browser = OpenBrowser(strURL)
If Not (browser.document.GetElementByID("ctl00$ContentPlaceHolder1$btnSubmit") Is Nothing) Then
        browser.document.GetElementByID("ctl00$ContentPlaceHolder1$btnSubmit").Click
Else
    MsgBox "Submit button Click failed"
    Wscript.Quit
End If

Function OpenBrowser(URL)
Dim ie
Set ie = CreateObject("InternetExplorer.Application")

ie.Visible = True
ie.Navigate2 URL
WaitForBrowserReadyStatus(ie)
Set OpenBrowser = ie

End Function

Private Sub WaitForBrowserReadyStatus(ie)
Const WAIT_TIMEOUT = 2000

While (ie.Busy) or (ie.ReadyState <> 4) 

    WScript.Sleep(WAIT_TIMEOUT)
  '    wait (1)
Wend
End Sub

I get the error:

0x80010108 - unknown exception

either at:

If Not (browser.document.GetElementByID("ctl00_ContentPlaceHolder1_btnSubmit") Is Nothing) Then

or at:

While (ie.Busy) or (ie.ReadyState <> 4)

This exact code works on another VBScript for a different website so I have no idea why it throws these exceptions.

internet-explorer
exception
vbscript
asked on Stack Overflow Jul 27, 2015 by user3020320 • edited Jul 27, 2015 by Bond

1 Answer

0

GetElementByID() can be tricky. If it finds the element, everything works fine and you can assign the object reference to a variable. But if it doesn't find the element, VBScript will throw an "Object Required" error when trying to perform an object assignment or object comparison (e.g., Is Nothing).

The trick is to use IsObject() to test if the value returned is truly an object. If so, perform the object assignment or carry on otherwise as normal.

If IsObject(browser.Document.getElementByID("ctl00$ContentPlaceHolder1$btnSubmit")) Then

    ' Object returned. ID was found in the DOM. Safe to assign.
    Dim e
    Set e = browser.Document.getElementByID("ctl00$ContentPlaceHolder1$btnSubmit")
    e.Click

Else

    ' Not an object. In other words, the ID wasn't found in the DOM.

End If
answered on Stack Overflow Jul 27, 2015 by Bond

User contributions licensed under CC BY-SA 3.0