获取传递到网站表单的数据



我正在尝试将数据传递到第四页上的形式中。 我已经成功地浏览了宏登录,根据输入的数据搜索特定页面,转到该特定页面,并拉出我要将数据输入到多个位置的表单。

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

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

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

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

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

  1. 您确实希望正确等待页面加载While .Busy Or .readyState < 4: DoEvents: Wend。如果你加入你的替补,你将在每次.Click/.Submit/.Navigate之后都需要这个
  2. 如果是时序问题,那么你可以有一个循环计时器,如图所示,循环直到达到元素集或超时 - 以避免无限循环
  3. 您确实想要测试是否设置了元素
  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

最新更新