【问题标题】:IE Automation (IE8) using VBA and Input type "hidden"使用 VBA 和输入类型“隐藏”的 IE 自动化 (IE8)
【发布时间】:2018-07-01 01:41:55
【问题描述】:

我正在使用 vba 在 Internet Explorer 中自动化站点。到目前为止,我已经非常成功。我遇到的问题是有一个输入字段,类型是“隐藏”而不是文本。我该如何解决这个问题?我知道一定有办法。

感谢您的任何建议。

【问题讨论】:

  • 我猜你正在处理网络表单,所以你能分享一下表单的 HTML 内容吗?还有你的 VBA 代码吗?
  • 如果您展示您尝试过的代码并准确解释您遇到了什么错误,或者什么不工作,那就更好了。通常,您将引用设置为隐藏的表单字段,您可以像设置常规文本输入字段一样设置其值。

标签: vba internet-explorer


【解决方案1】:

下面的例子演示了如何在循环中通过索引引用输入元素:

Sub Test()
    Set objIE = CreateObject("InternetExplorer.Application")
    objIE.Visible = True
    ' navigate and download the web page
    objIE.Navigate "https://www.yahoo.com/"
    Do While objIE.ReadyState <> 4 Or objIE.Busy
        DoEvents
    Loop
    ' retrieve document object
    Set objDocument = objIE.document
    ' retrieve forms collection
    Set colForms = objDocument.forms
    ' retrieve the first form object by index
    Set objForm = colForms(0)
    ' retrieve the form input tags collection
    Set colInputTags = objForm.getElementsByTagName("input")
    ' loop through all input tags in the form
    For n = 0 To colInputTags.Length - 1
        ' refer to the certain input tag by index
        Set objInputTag = colInputTags(n)
        ' output
        Debug.Print n _
            & " (" & objInputTag.Type & ") " _
            & objInputTag.Name & " = " _
            & objInputTag.Value
    Next
    ' refer to input tag #0
    Set objElement = colInputTags(0)
    ' hide the search inputbox
    objElement.Type = "hidden"
    ' refer to input tag #4
    Set objElement = colInputTags(4)
    ' output the current value
    Debug.Print "#4 value = " & objElement.Value
    ' cnange the value
    objElement.Value = "input tag #4"
    ' cnange the type
    objElement.Type = "Text"
    ' refer to input tag #5
    Set objElement = colInputTags(5)
    ' cnange the value
    objElement.Value = "input tag #5"
    ' cnange the type
    objElement.Type = "Text"
    ' quit IE
    objIE.Quit
End Sub

代码为我提供了以下输出:

如你所见,它隐藏了主输入标签,输出隐藏的输入标签#4的值,并改变#4和#5的类型和值,所以网页看起来像:

【讨论】:

  • 非常感谢您的帮助。我仍然对如何将文本元素放入“隐藏”类型的输入框中感到困惑,我似乎无法改变该值。
  • @hockeybo97,我添加了描述如何更改隐藏的输入内容。
  • 非常感谢。我已经获得了成功改变的价值。我现在的挑战是如何将它放入表单的输入框中。这是源代码的示例。 我想让 Input Name 等于我指定的值。
  • 无论我如何编码。我无法让输入框填充值。有没有办法用 vba 将源更改为 type = "Text"?
  • @hockeybo97,我添加了一个关于如何更改输入元素的类型和值的示例。如果您需要进一步阐述,请更新您的答案:添加您运行的代码、结果并描述问题所在。
【解决方案2】:

CSS 选择器:

您可以使用 CSS 选择器来定位 input[type='hidden'] 的那些元素。这读作带有input 标签的元素,具有type 属性,值为'hidden'


CSS 查询(示例结果):


前表(示例):


工作表(示例):


VBA:

您使用documentquerySelectorAll 方法应用css 选择器,然后循环返回的nodeList 的长度。

我通过将"hidden" 值替换为vbNullString 并使用value 属性分配一个值来使其可见。

Option Explicit
Public Sub AlterHidden()
    Dim ie As New InternetExplorer, aNodeList As Object, i As Long
    Const URL As String = "https://uk.yahoo.com"
    With ie
        .Visible = True
        .navigate URL
        While .Busy Or .readyState < 4: DoEvents: Wend
        On Error GoTo noCookieRequest
        .document.querySelector("input[value='OK']").Click '<==On time required for cookies
        While .Busy Or .readyState < 4: DoEvents: Wend
noCookieRequest:
       Do
        On Error Resume Next
        Set aNodeList = .document.querySelectorAll("input[type='hidden']")
        On Error GoTo 0
        Loop While aNodeList Is Nothing
        For i = 0 To aNodeList.Length - 1
            With aNodeList.item(i)
                .Type = vbNullString
                .Value = "Surprise " & i
            End With
        Next i
        Stop                                     '<== Delete me
        .Quit
    End With
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-28
    • 2021-01-07
    • 1970-01-01
    • 2020-01-16
    相关资源
    最近更新 更多