您收到该错误是因为您没有给它足够的时间来验证FROM
和TO
下拉列表的名称(是 - 不是文本框)
试试这个代码(尝试和测试)。我也在使用 IE 的后期绑定。根据您的代码进行更改。
给下拉列表中的Sleep 5000
文本足够的时间来使用下拉列表验证自身。
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub Click_Btn()
Dim objForms As Object
Dim vTxtInput As Variant
Dim objIE As Object
Set objIE = CreateObject("InternetExplorer.Application")
'Make sure an IE object was hooked
If TypeName(objIE) = "Nothing" Then
MsgBox "Could not hook Internet Explorer object", _
vbCritical, "GetFields() Error"
'GoTo Clean_Up
End If
objIE.Navigate "http://fly3.emirates.com/CAB/IBE/SearchAvailability.aspx"
objIE.Visible = True
Sleep 5000
Set objForms = objIE.document.all
'~~> Choose one way Flights
objIE.document.getElementById("ctl00_c_CtWNW_onewaySearch").Click
'~~> From
objIE.document.getElementById("ctl00_c_CtWNW_ddlFrom-suggest").Focus
objIE.document.getElementById("ctl00_c_CtWNW_ddlFrom-suggest").Value _
= "Mumbai (BOM)"
Sleep 5000
'~~> Departure Date
objIE.document.getElementById("ctl00_c_CtWNW_txtDepartDate").Focus
objIE.document.getElementById("ctl00_c_CtWNW_txtDepartDate").Value _
= "28 Sep 12"
Sleep 5000
'~~> To
objIE.document.getElementById("ctl00_c_CtWNW_ddlTo-suggest").Focus
objIE.document.getElementById("ctl00_c_CtWNW_ddlTo-suggest").Value _
= "Sydney (SYD)"
Sleep 5000
objIE.document.getElementById("ctl00_c_FS_FF").Focus
objIE.document.getElementById("ctl00_c_FS_FF").Click
End Sub
编辑
这是Way 2
,它比我们不用于验证Way 1 (Above)
的速度更快。这不需要您验证下拉菜单。它的作用是绕过在提交按钮的 ONCLICK 事件中执行的 javascript。15 Seconds
Sleep 5000
checkValidation();
方式 2
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub Click_Btn()
Dim objForms As Object
Dim vTxtInput As Variant
Dim objIE As Object
Set objIE = CreateObject("InternetExplorer.Application")
'Make sure an IE object was hooked
If TypeName(objIE) = "Nothing" Then
MsgBox "Could not hook Internet Explorer object", _
vbCritical, "GetFields() Error"
'GoTo Clean_Up
End If
objIE.Navigate "http://fly3.emirates.com/CAB/IBE/SearchAvailability.aspx"
objIE.Visible = True
Sleep 5000
Set objForms = objIE.document.all
'Choose one way Flights
objIE.document.getElementById("ctl00_c_CtWNW_onewaySearch").Click
' From
objIE.document.getElementById("ctl00_c_CtWNW_ddlFrom-suggest").Focus
objIE.document.getElementById("ctl00_c_CtWNW_ddlFrom-suggest").Value _
= "Mumbai (BOM)"
' Departure Date
objIE.document.getElementById("ctl00_c_CtWNW_txtDepartDate").Focus
objIE.document.getElementById("ctl00_c_CtWNW_txtDepartDate").Value _
= "28 Sep 12"
' To
objIE.document.getElementById("ctl00_c_CtWNW_ddlTo-suggest").Focus
objIE.document.getElementById("ctl00_c_CtWNW_ddlTo-suggest").Value _
= "Sydney (SYD)"
objIE.document.getElementById("ctl00_c_FS_FF").Focus
objIE.document.getElementById("ctl00_c_FS_FF").onclick = _
Replace(objIE.document.getElementById("ctl00_c_FS_FF").onclick, _
"checkValidation();", "true;")
objIE.document.getElementById("ctl00_c_FS_FF").Click
End Sub