【问题标题】:Retrieve the value of a read-only input text box using VBScript使用 VBScript 检索只读输入文本框的值
【发布时间】:2019-09-12 02:18:13
【问题描述】:

我正在使用基于 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标签有关?

【问题讨论】:

  • 看起来你的objShell 没有初始化。
  • objShell 在代码顶部初始化为Set objShell = CreateObject("Shell.Application"),我只是没有将它包含在示例中以节省空间以提高可读性。此外,它必须被初始化,因为我可以找到URL,只是无法访问input 字段中的数字。
  • 足够完整是有帮助的,而且一定要清楚地指出哪里出错了。
  • 我很抱歉。下次我必须记住这一点。当我说“我能够设置objIE 对象并找到URL...”时,我以为我在我的示例之后很清楚。我不相信我需要显示初始化objShell,因为它会在For...Next 循环的开头显示错误。感谢您的建议。

标签: html vbscript


【解决方案1】:

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

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

【讨论】:

  • 我尝试了你的建议,我收到了Run-time error '13': Type mismatch。任何想法为什么?
  • Set EN_ID = objIE.document.getElementById("sys_readonly.x_opt_im_issue_task.number")。我在你提到的那行前面添加了Set,并删除了.Value,然后单步执行了代码,它一直在该行失败。
  • 您自己尝试过我的脚本吗?在您的示例中,您之前是否将 EN_ID 分配/声明为其他内容?我需要查看更多代码。
  • 我更新了我的代码。希望你能看到我看不到的东西。
  • 当我复制您的代码,将 URL 更改为本地 URL(以便我可以测试)时,它对我来说运行良好,没有错误。唯一不同的是,我正在使用普通的 html/head/body 标签和您的 div 结构测试本地网页。
【解决方案2】:

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

我感觉iframe 标签可能会阻止我检测到input 字段ID,所以我查看了iframe,发现input 字段中的TSK 编号也在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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多