VBA-无法将数据传递到网站表单

时间:2018-10-15 23:46:31

标签: excel vba excel-vba web-scraping

我正在尝试将数据传递到第四页上的表单中。我已经成功地通过宏进行了登录,并根据输入的数据搜索特定的页面,转到该特定的页面,然后将要在其中输入数据的表格拉到多个位置。

Sub Test()
    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    IE.Navigate "http://mywebsite.com/Pages/MainSite/Default.aspx"
    Do While IE.Busy
        Application.Wait DateAdd("s", 1, Now)
    Loop
    IE.document.getElementById("txtUserName").Value = "dummyusername"
    IE.document.getElementById("txtPassword").Value = "password"
    IE.document.getElementById("btnLogin").Click
End Sub

Sub Next2()
    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    IE.Navigate "mywebsite.com/pages/hr/TimeEntryDashboard.aspx"
    Do While IE.Busy
        Application.Wait DateAdd("s", 2, Now)
    Loop
    IE.document.getElementById("MainContent_MainContent_txtStartDateFilter").Value = "10/13/2018"
    IE.document.getElementById("MainContent_MainContent_txtEmployeeNameFilter").Value = "122631"
    IE.document.getElementById("MainContent_MainContent_btnFind").Click
End Sub

Sub Next3()
    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    IE.Navigate "mywebsite.com/pages/hr/TimeAndInspectionWizard.aspx?id=0245b750-4cde-47da-a754-fb7f8bfecfc9"
    Do While IE.Busy
        Application.Wait DateAdd("s", 1, Now)
    Loop
    IE.document.getElementById("imgGridTimeDetailsArrow0").Click
    IE.document.getElementById("MainContent_MainContent_tcMain_tpValidation_rptInspectionDetails_imbGridInspectionDetailsOptionsAdd_0").Click
End Sub

Sub Next4()
    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")
  ' IE.Visible = True
  ' IE.Navigate "mywebsite.com/pages/hr/TimeAndInspectionWizard.aspx?id=0245b750-4cde-47da-a754-fb7f8bfecfc9"
    Do While IE.Busy
        Application.Wait DateAdd("s", 2, Now)
    Loop
    IE.document.getElementById("MainContent_MainContent_tcMain_tpInspectionDetails_txtInspectionDetailsLotNumber").Value = "12345"
    IE.document.getElementById("MainContent_MainContent_tcMain_tpInspectionDetails_txtInspectionInspectedQuantity").Value = "1"
    IE.document.getElementById("MainContent_MainContent_tcMain_tpInspectionDetails_txtGoodWithoutReworkQuantity").Value = "1"
    IE.document.getElementById("MainContent_MainContent_tcMain_tpInspectionDetails_txtAddInspectionResult").Click
End Sub

上面的这一部分在应该将数据输入到屏幕的第一部分产生了错误。

2 个答案:

答案 0 :(得分:1)

作为代码验证的一部分,您必须检查元素是否存在,这也将使您以更好的方式识别问题。例如,这是用验证重写的代码。

Sub Next4()
    Dim IE As Object, Elem As Object
    Set IE = CreateObject("InternetExplorer.Application")

    With IE
        Do While .Busy
            Application.Wait DateAdd("s", 2, Now)
        Loop

        With .document

            Set Elem = .GetElementById("MainContent_MainContent_tcMain_tpInspectionDetails_txtInspectionDetailsLotNumber")
            If Not Elem is nothing Then
                Elem.Value = "12345"
                Set Elem = Nothing
            End If

            Set Elem = .GetElementById("MainContent_MainContent_tcMain_tpInspectionDetails_txtInspectionInspectedQuantity")
            If Not Elem is nothing Then
                Elem.Value = "1"
                Set Elem = Nothing
            End If

            Set Elem = .GetElementById("MainContent_MainContent_tcMain_tpInspectionDetails_txtGoodWithoutReworkQuantity")
            If Not Elem is nothing Then
                Elem.Value = "1"
                Set Elem = Nothing
            End If

            Set Elem = .GetElementById("MainContent_MainContent_tcMain_tpInspectionDetails_txtAddInspectionResult")
            If Not Elem is nothing Then
                Elem.Click
            Else
                MsgBox "Can't Click as Element Missing : " & "MainContent_MainContent_tcMain_tpInspectionDetails_txtAddInspectionResult"
            End If
       End With
    End With
End Sub

答案 1 :(得分:1)

您尚未指出错误或未显示任何HTML。可能是您的元素位于父框架/ iframe中。

  1. 您确实希望适当等待页面加载While .Busy Or .readyState < 4: DoEvents: Wend。如果您还加入了潜艇,则每个.Click/.Submit/.Navigate
  2. 之后都需要此潜艇
  3. 如果这是一个计时问题,那么您可以设置一个循环计时器,如图所示,循环直到达到元素设置或超时-避免无限循环
  4. 您确实想测试是否设置了元素
  5. 您确实希望尝试分配包装在On Error Resume Next中的对象引用,以保持代码运行并防止用户在未设置元素的情况下看到错误消息。
  6. 您的问题是关于第一部分的,因此我已经解决了,但是我在评论中回荡了您的看法,即您只需要一个IE实例,并且应该与之配合使用,除非这些子项无关且独立运行。您仍然可以考虑在这种情况下是否可以使用一个实例将它们加入。

VBA:

Option Explicit
Public Sub LoopUntilSet()
    Dim IE As New InternetExplorer, t As Date
    Const MAX_WAIT_SEC As Long = 5

    With IE
        .Visible = True
        .navigate "yourURL"

        While .Busy Or .readyState < 4: DoEvents: Wend

        t = Timer
        Do While x Is Nothing
            DoEvents
            On Error Resume Next
            Set ele = .document.getElementById("txtUserName")
            On Error GoTo 0
            If Timer - t > MAX_WAIT_SEC Then Exit Do
        Loop

        If ele Is Nothing Then
            Exit Sub
        Else
            ele.Value = "dummyusername"
            'other code
        End If
        .Quit
    End With
End Sub