我正在尝试将数据传递到第四页上的表单中。我已经成功地通过宏进行了登录,并根据输入的数据搜索特定的页面,转到该特定的页面,然后将要在其中输入数据的表格拉到多个位置。
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
上面的这一部分在应该将数据输入到屏幕的第一部分产生了错误。
答案 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中。
While .Busy Or .readyState < 4: DoEvents: Wend
。如果您还加入了潜艇,则每个.Click/.Submit/.Navigate
On Error Resume Next
中的对象引用,以保持代码运行并防止用户在未设置元素的情况下看到错误消息。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