【问题标题】:VBScript not running depending on the web serverVBScript 未根据 Web 服务器运行
【发布时间】:2016-06-01 08:51:28
【问题描述】:

我对 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 对象,但我不知道为什么它只在其中一台服务器中丢失。

【问题讨论】:

  • 我不确定 IIS 与此有什么关系,您正在运行的 VBS 代码是客户端 (此实例中的服务器是客户端),它有据我所知,对 IIS 没有影响??
  • 很可能其中一台服务器没有安装 Excel 或至少没有安装 COM 组件。无论哪种方式,您都需要进行一些调试以找出未设置的对象。
  • @Lankymart 是正确的。服务器与此无关:脚本就像一个人在 Web 应用程序中执行某些操作。但是,取决于我访问网络应用程序的服务器,它是否可以正常工作。服务器不需要安装 Excel 或 COM 组件,因为如果我访问位于运行脚本的服务器中的 Web 会引发错误,那么 Web 应用程序完全可以通过人工交互来使用。
  • 什么?!?抱歉,我在 处丢失了您“服务器不需要安装 Excel 或 COM 组件,因为如果我访问位于运行脚本的服务器中的 Web 会引发错误,那么 Web 应用程序对人类完全有用互动”...
  • iis-7iis-7.5IIS 设置 中可能有所不同?对不起,我不太擅长IIS话题;你可以通过this question的链接向超级用户提问。

标签: vbscript iis-7 automation iis-7.5


【解决方案1】:

getElementById 方法返回与指定值具有相同ID 属性的第一个对象,如果找不到id,则返回null。参考:.NET FrameworkDocument Object Model (DOM)
另一方面,Is operator 比较两个 object 引用变量,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\SO\37563820.vbs
1 Null    Null
9 Nothing Nothing
     Null  = Nothing
not (Null  = Nothing)
     Null is Nothing
NOT (Null is Nothing)
==> D:\VB_scripts\SO\37563820.vbs(12, 1) Microsoft VBScript runtime error: Object required

【讨论】:

  • 为什么要引用 .Net 方法?他们甚至在 OP 代码中的哪个位置尝试比较 Null?我不完全确定你对这个问题的理解。缺少对象引用通常是由于环境中未设置的依赖项(COM 等)。 OP甚至说它适用于一个而不是另一个。那么这有什么意义呢?
  • @Lankymart 他们甚至在 OP 代码中的哪个位置尝试比较 Null 无论第 295 行中的哪个 .document.getElementByID(…) 必须返回Null:请注意,我的最后一个代码 sn-p 显示完全相同的错误。另一方面,最重要的一个可以显示如何从提示开始:无论哪种方式,您都需要进行一些调试以找出未设置的对象
  • 有效!!但我不明白为什么我的原始代码适用于位于其中一台服务器而不是另一台服务器中的页面......
  • @Carlos 您确定页面在所有服务器上都相同吗?那么 MSIE 版本呢?客户端 VBScript 现在被认为已过时且已弃用,并且可能无法在任何现代浏览器(包括较新版本的 MSIE)中运行。
  • @JosefZ 是的,我确定页面是一样的,因为我已经完成了复制粘贴。该脚本在同一台计算机上运行,​​因此 MSIE 版本相同。这两个网页之间的唯一区别是它们所在的服务器......
猜你喜欢
  • 2020-03-04
  • 1970-01-01
  • 2013-08-26
  • 1970-01-01
  • 2014-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多