使用 VBScript 检索只读输入文本框的值

Retrieve the value of a read-only input text box using VBScript

我正在使用基于 Internet Explorer 的应用程序,我需要在其中检索只读输入文本框的值。我查看了其他 Stack Overflow 问题,但没有看到任何有关获取只读或隐藏输入框的值的信息。我也没有在互联网上找到任何可以使用的东西。

这是我试图从中获取值的输入框的 HTML 代码:

<div class="col-xs-10 col-sm-9 col-md-6 col-lg-5 form-field input_controls">
   <div class="hidden" ng-non-bindable="">
      <input name="sys_original.x_opt_im_issue_task.number" id="sys_original.x_opt_im_issue_task.number" type="hidden" value="TSK0111065" />
   </div>
   <input class="form-control disabled " id="sys_readonly.x_opt_im_issue_task.number" readonly="readonly" ng-non-bindable="" value="TSK0111065" />
</div>

这是我试图用来获取不起作用的输入文本框的值的 VBScript 代码:

已更新

Option Explicit

Dim WShell, objShell, objIE
Dim strMessage, URL, ErrMsg, URLFound, Browser
Dim EN_ID, EntNowID

Sub Main()
   On Error Resume Next
      Set WShell = CreateObject("WScript.Shell")
      Set objShell = CreateObject("Shell.Application")
      If Err.Number <> 0 Then ShowError("Failed to create objects")
   On Error GoTo 0

   Check_EN

   SetEverythingToNothing
End Sub

'---------------------------------

Sub ShowError(strMessage)
   MsgBox strMessage & vbNewline & vbNewline & "Error number: " & Err.Number & vbNewline & "Source: " & Err.Source & vbNewline & "Description: " & Err.Description
   Err.Clear

   SetEverythingToNothing
End Sub

'------------------------------

Sub Check_EN()
   URL = "https://enterprisenow.mysite.com"
   ErrMsg = "EnterpriseNOW is not open or on the incorrect page. Please check & rerun the macro."

   Check_URL

   ErrMsg = ""

   Set EN_ID = objIE.document.getElementById("sys_readonly.x_opt_im_issue_task.number")
   EntNowID = EN_ID.Value

   MsgBox EntNowID
End Sub

'------------------------------

Function Check_URL()
   URLFound = False

   For Each Browser In objShell.Windows()
      If InStr(UCase(Browser.LocationURL), UCase(URL)) > 0 Then
         If InStr(UCase(Browser.FullName), "IEXPLORE.EXE") Then
            If Err.Number = 0 Then
               Set objIE = Browser
               URLFound = True
               Exit For
            End If
         End If
      End If
   Next

   If URLFound = False Then
      MsgBox "Unable to find URL."
      SetEverythingToNothing
   End If
End Function

'-----------------------------

Sub SetEverythingToNothing()

   Set WShell = Nothing
   Set objShell = Nothing
   Set Browser = Nothing
   Set objIE = Nothing

End Sub

我能够设置 objIE 对象并找到 URL,但我收到 "Run-time error '424': Object required"。是因为输入文本框被隐藏还是只读?我也想知道它是否与嵌套 div 标签有关?

尝试以下方法 - 它对我有用。我注意到,如果我访问:

objIE.document.getElementById("sys_readonly.x_opt_im_issue_task.number").Value

我不止一次收到错误:需要对象

但是,如果我使用 'Set' 并创建了一个像这样的对象:

Set tasknumber = objIE.document.getElementById("sys_readonly.x_opt_im_issue_task.number")

然后我可以多次访问该值而不会收到错误。

Option Explicit

Dim objShell : Set objShell = CreateObject("shell.application")
Dim URL : URL = "https://enterprisenow.mysite.com/"
Dim URLFound : URLFound = False
Dim Browser, tasknumber, tasknumbervalue

For Each Browser In objShell.Windows()
    If InStr(UCase(Browser.FullName), "IEXPLORE.EXE") Then
        If InStr(UCase(Browser.LocationURL), UCase(URL)) > 0 Then   
            If Err.Number = 0 Then

                Set tasknumber = Browser.document.getElementById("sys_readonly.x_opt_im_issue_task.number")

                If (NOT IsNull(tasknumber) And NOT tasknumber Is Nothing) Then
                    tasknumbervalue = tasknumber.value
                End If

                Set tasknumber = Nothing                

                Exit For
            End If
        End If
    End If
Next

MsgBox tasknumbervalue

Set Browser = Nothing
Set objShell = Nothing

好吧,这不是我希望的解决方案,但它比尝试从 input 字段中获取数字更容易。

我觉得 iframe 标签可能会阻止我检测 input 字段 ID,所以我查看了 iframe 并看到 TSK 编号在input 字段也在 iframe 标签中。

而不是尝试这个:

EN_ID = objIE.document.getElementById("sys_readonly.x_opt_im_issue_task.number").Value

我能够使用这个:

iframeName = Split(objIE.document.getElementById("gsft_main").Title, " | ")    'Title = "TSK0111065 | Issue Task | ServiceNow"
EN_ID = iframeName(0)