【问题标题】:Using Excel to submit information to a website form and parse the results使用 Excel 将信息提交到网站表单并解析结果
【发布时间】:2016-01-25 20:28:26
【问题描述】:

下午好,

我正在尝试设置一种为内部网站自动生成报告的方法。我在一所大学任教,需要通过大学提供的界面从教师可访问的数据库中提取一些数据。因此,我正在尝试访问我有权查看的资料,需要登录才能查看,但不想重复这些步骤可能数百次。

我的想法是:

1) 使用带有我的凭据的 IE 登录大学提供的界面(因此系统知道我已获得授权)。

2) 向 excel 提供我要跟踪的学生的 ID 号列表

3) 使用 VBA 遍历每个 ID,执行以下步骤:

  • 将 Excel 列表中的 ID 号输入到相应的表单文本框中
  • 点击“提交”按钮
  • 点击结果页面上的辅助“提交”按钮
  • 复制出现的 HTML 文本页面,并解析出我需要的信息
  • 移动到下一个数字并重复

我已经尝试了一些我见过的其他选项(例如 VBA to Enter Data Online and Submit Form),但在语法上遇到了错误。

我试图引用的网站上的 HTML 编码的相关部分是:

<FORM ACTION="action" METHOD="POST" NAME="idinputform">
Enter Number Here: 
<INPUT TYPE="number" NAME="ID_NUM" SIZE="9" MAXLENGTH="9">
<INPUT TYPE="hidden" NAME="refresh_proc" VALUE="menu_1">
<INPUT TYPE="submit" VALUE="Submit"> <INPUT TYPE="reset" VALUE="Reset">
</FORM>

我在 Excel 中得到的是:

Sub QueryInfo()
    Application.ScreenUpdating = False
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    IE.Navigate "website address"
    IE.Document.idinputform.number.Value = "000000000" 
    Application.ScreenUpdating = True
End Sub

所以,我首先打开一个 IE 窗口并登录该界面,然后启动 VBA。应用程序运行,打开一个新的 IE 窗口,点击该网站(它保留了我从第一个网站开始的登录权限),然后崩溃并出现未指定的错误。

看起来这应该很简单,但我只是没有看到它。

谢谢! -G-

【问题讨论】:

  • 试试这个IE.document.getElementsByName("idinputform").Item(0).Value = "myID",其中myID = 学生ID的单元格值或您目前硬编码的任何ID。
  • 唉,不。 IE.document.getElementsByName("idinputform").Item(0).Value = "myID" 不起作用。 IE.document.getElementByName("number").Value = "myID" 也没有。其他想法?
  • 啊,我看错了getElementByName("ID_NUM").Item(0).Value
  • 仍然没有快乐,但开始对我的困惑感觉好多了:-) IE.document.getElementByName("ID_NUM").Item(0).Value = "myID" 生成错误 - 运行 -时间错误“-2147467259 (80004005)”:自动化错误/未指定的错误。与此同时,我将开始循环使用 Jeeped 的方法。感谢您的持续帮助!

标签: excel forms vba internet-explorer


【解决方案1】:

在处理集合(多个对象)时,我发现最好遍历可用对象,并在执行过程中对每个对象进行测试。

Sub QueryInfo()
    Dim ie As Object, iFRM As Long, iNPT As Long
    'Application.ScreenUpdating = False   'uncomment this once it is working
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True
    ie.Navigate "website address"

    'wait untli the page loads
    Do While ie.busy Or ie.readyState <> 4  'READYSTATE_COMPLETE = 4
        DoEvents
    Loop

    With ie.document.body
        For iFRM = 0 To .getElementsByTagName("form").Length - 1
            If LCase(.getElementsByTagName("form")(iFRM).Name) = "idinputform" Then
                With .getElementsByTagName("form")(iFRM)
                    For iNPT = 0 To .getElementsByTagName("input").Length - 1
                        Select Case LCase(.getElementsByTagName("input")(iNPT).Name)
                            Case "id_num"
                                .getElementsByTagName("input")(iNPT).Value = 123
                            Case "refresh_proc"
                                .getElementsByTagName("input")(iNPT).Value = "menu_2"
                        End Select
                    Next iNPT
                    .submit    '<~~ submit the form
                    Do While ie.busy Or ie.readyState <> 4: DoEvents: Loop
                    Exit For
                End With
                Exit For
            End If
        Next iFRM
    End With

    With ie.document.body
        'should be at the form's destination
    End With

    Application.ScreenUpdating = True
End Sub

页面上可能有多个表单。遍历每一个,直到找到具有正确名称的那个。在该表单定义中,有多个输入元素;循环遍历每个并根据需要应用参数。完成后,提交表单并退出循环。

【讨论】:

    【解决方案2】:

    我曾经有一段时间无法通过使用 HTML 元素来让表单提交工作...我找到了一个很好的解决方法,即找出站点将其设置为焦点的位置,然后使用击键以使输入成为必要。

    我承认这并不优雅,但它让我绕过了一个让我熬夜的僵局。

    Application.Wait (DateAdd("S", 1, Now()))
    DoEvents
        Call SendKeys("{TAB}", True)
    Application.Wait (DateAdd("S", 2, Now()))
    Text2Clipboard (Format(Password1, "00000000"))
    Application.Wait (DateAdd("S", 1, Now()))
    DoEvents
        Call SendKeys("^v", True)
    Application.Wait (DateAdd("S", 1, Now()))
    DoEvents
        Call SendKeys("{TAB}", True)
    Application.Wait (DateAdd("S", 2, Now()))
    Text2Clipboard (Password2)
    Application.Wait (DateAdd("S", 1, Now()))
    DoEvents
        Call SendKeys("^v", True)
    Application.Wait (DateAdd("S", 1, Now()))
    DoEvents
        Call SendKeys("~", True)
    Application.Wait (DateAdd("S", 2, Now()))
    Dim Counter As Integer
    Counter = 0
    Do While IE.Busy
        Application.Wait (DateAdd("S", 5, Now()))
    Loop
    Application.Wait (DateAdd("S", 2, Now()))
    Dim links, link As Object
    Set links = IE.Document.getElementById("t-mainmenu").getElementsByTagName("a")
    links(1).Click
    Do While IE.Busy
        Application.Wait (DateAdd("S", 5, Now()))
    Loop
    

    【讨论】:

      猜你喜欢
      • 2019-11-05
      • 1970-01-01
      • 2011-08-05
      • 2012-11-20
      • 1970-01-01
      • 2012-03-28
      • 2012-01-12
      • 2013-05-06
      • 1970-01-01
      相关资源
      最近更新 更多