使用VBscript 0x80010108自动化IE - 未知异常

时间:2015-07-27 22:32:12

标签: internet-explorer exception vbscript

我正在尝试使用VBScript点击网页上的按钮。我的其余部分工作得很好。但是在使用浏览器对象的不同实例中,我得到了一个例外。相同的代码适用于不同网站的另一个脚本。

这是我的代码:

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

我收到错误:

  

0x80010108 - 未知异常

在:

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

或在:

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

这个确切的代码适用于另一个不同网站的VBScript,所以我不知道它为什么抛出这些异常。

1 个答案:

答案 0 :(得分:0)

$s.Substring(0,$x) + $s.Substring($z) 可能很棘手。如果找到元素,一切正常,您可以将对象引用分配给变量。但是如果它没有找到该元素,VBScript在尝试执行对象赋值或对象比较时会抛出GetElementByID()错误(例如,"Object Required")。

诀窍是使用Is Nothing来测试返回的值是否真的是一个对象。如果是,请执行对象分配或正常进行。

IsObject()