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

问题描述 投票:1回答:2

我试图将数据传递到第四页上的表单。我已经成功浏览了宏,并根据输入的数据搜索特定页面,转到该特定页面,然后将我想要输入数据的表单拉到多个位置。

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

上面这部分是在第一部分产生错误,它应该在屏幕上输入数据。

excel vba excel-vba web-scraping
2个回答
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
投票

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

  1. 你确实想要一个正确的等待页面加载While .Busy Or .readyState < 4: DoEvents: Wend。如果你加入你的潜水员,你将需要在每个.Click/.Submit/.Navigate之后
  2. 如果它是一个计时问题,那么你可以有一个循环计时器,如图所示,循环直到元素设置或超时达到 - 以避免无限循环
  3. 您确实需要测试if元素是否已设置
  4. 您确实希望尝试分配包含在On Error Resume Next中的对象引用以保持代码运行,并防止用户在未设置元素时看到错误消息。
  5. 你的问题是关于第一部分,所以我已经解决了,但我回应了评论中的情绪,你只需要一个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
© www.soinside.com 2019 - 2024. All rights reserved.