VBScript 不是 运行 取决于网络服务器

VBScript not running depending on the web server

我对 VBScript 有一个非常奇怪的问题:它工作正常取决于 Web 所在的服务器。

我在两台装有 IIS 7.5 的服务器上有相同的 Web 应用程序。在每个服务器中,代码完全相同。

VBScript 执行一些 Excel 行并更新 Web 应用程序中的一些信息。更新此信息时出现问题。在其中一台服务器上没有问题,但在另一台服务器上出现以下错误:

脚本运行正常,我猜没有语法错误

抛出错误的代码是:

With objIE
    If (testing) Then
        .Navigate URLtesting
    Else
        .Navigate URL
        WScript.Sleep 2000
    End If

    WaitWebLoad()
    .document.getElementById(user).Value = userScript
    .document.getElementById(password).Value = passwordScript
    .document.getElementById(logInButton).Click()
    WaitWebLoad()
    .document.getElementByID(loretoLink).Click()
    WaitWebLoad()
    .document.getElementByID(updatePLLink).Click()
    WaitWebLoad()

    Do
        lastRow = activeSheet.Cells(rowIndex, columnPL_ID).Value
        dateAssociated = activeSheet.Cells(rowIndex, columnArrivalDate).Value
        concesion = activeSheet.Cells(rowIndex, columnConcesion).Value
        If lastRow <> "" Then
            .document.getElementByID(searchPL_TB).Value = lastRow
            .document.getElementByID(searchPL_Button).Click()
            WaitWebLoad()
            If (Not (.document.getElementByID(errorLookingPL)) Is Nothing Or Not (.document.getElementByID(noSearchResult) Is Nothing)) Then
                'PL not found
                recordsNotFound = recordsNotFound + 1
                WriteOpenFileText(logFilePath), (Now & vbTab & "***PL not found: " & lastRow & " - [" & dateAssociated & "]" & " - " & concesion & vbCrLf)
            Else ...

第295行是:

If (Not (.document.getElementByID(errorLookingPL)) is Nothing Or Not (.document.getElementByID(noSearchResult) is Nothing)) Then

WaitWebLoad函数代码为:

Function WaitWebLoad()
    Dim timer
    timer = 0

    With objIE
        Do While .Busy
            If timer < timerWebLoad Then
                WScript.Sleep 100
                timer = 100
            Else
                'Error loading the web
                objExcel.Workbooks.close
                objExcel.quit
                objIE.quit
                DeleteFile(pathTemp)
                endScriptStamp = Now

                WScript.Echo("Error." & vbCrLf & "Total runtime is: " & DateDiff("s", startScriptStamp, endScriptStamp) & " seconds.")
                WScript.quit
            End If
        Loop
    End With
End Function

我想问题是脚本,当 运行 发生错误的服务器时,正在丢失 objIE 对象,但我不知道为什么它只在其中一台服务器。

getElementById 方法 returns 第一个 对象 具有与指定值相同的 ID 属性,或 null 如果找不到 id。参考:.NET Framework and Document Object Model (DOM).
另一方面,Is operator比较两个对象引用变量,Null不是对象;它表示变量不包含有效数据。
此外,如果在 VBScript 中使用,getElementById 方法可能会引发 800A01A8 (=decimal -2146827864) Microsoft VBScript 运行时错误:Object required 如果使用不当:例如,因为你不能写 Set objAny = Null!

这里有一个解决方法:

On Error Resume Next                                              ' enable error handling
  Set oerrorLookingPL = .document.getElementByID(errorLookingPL)  ' try
  If Err.Number <> 0 Then Set oerrorLookingPL = Nothing           ' an error occurred?
  Err.Clear
  If Vartype(oerrorLookingPL) = 1 Then Set oerrorLookingPL = Nothing   ' Null => Nothing
  Set onoSearchResult = .document.getElementByID(noSearchResult)
  If Err.Number <> 0 Then Set onoSearchResult = Nothing
  Err.Clear
  If Vartype(onoSearchResult) = 1 Then Set onoSearchResult = Nothing
On Error Goto 0                                                   ' disable error handling
If (Not (oerrorLookingPL Is Nothing) Or Not (onoSearchResult Is Nothing)) Then

我不建议全局使用 On Error Resume Next,因为 If condition Then … 语句总是在给定 conditionTrue 的情况下进行求值,如果在求值时发生运行时错误,请参阅下一步示例:

On Error Resume Next
' show Variant subtype information about Null and Nothing
Wscript.Echo VarType(Null)    & " Null    " & TypeName(Null)
Wscript.Echo VarType(Nothing) & " Nothing " & TypeName(Nothing)
' all conditions as well as their negations are evaluated to `True`
if     (Null  = Nothing) then Wscript.Echo "     Null  = Nothing"
if NOT (Null  = Nothing) then Wscript.Echo "not (Null  = Nothing)"
if     (Null is Nothing) then Wscript.Echo "     Null is Nothing"
if NOT (Null is Nothing) then Wscript.Echo "not (Null is Nothing)"
' show runtime error
On Error GoTo 0
if     (Null is Nothing) then Wscript.Echo "     Null is Nothing"

Null = NothingNull is Nothing 条件都被评估为 True 以及它们的否定!

==> cscript D:\VB_scripts\SO563820.vbs
1 Null    Null
9 Nothing Nothing
     Null  = Nothing
not (Null  = Nothing)
     Null is Nothing
NOT (Null is Nothing)
==> D:\VB_scripts\SO563820.vbs(12, 1) Microsoft VBScript runtime error: Object required