2

我正在尝试通过在表单输入文本框中提供所有值来提交网络表单,但是当我从 excel vba 调用提交按钮单击时,其中一个文本框变为空并引发验证错误。

Sub Click_Btn()    
  Dim objForms As Object
  Dim vTxtInput As Variant
  'Set objIE = GetIEApp
  Set objIE = New InternetExplorer

  '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
  Sleep 1000

  objIE.document.getElementById("ctl00_c_CtWNW_ddlTo-suggest").Value = "Sydney (SYD)"

  ' Departure
  objIE.document.getElementById("ctl00_c_CtWNW_ddlFrom-suggest").Value = "Mumbai (BOM)"

  ' Departure Date
  objIE.document.getElementById("ctl00_c_CtWNW_txtDepartDate").Value = "28 Sep 12"
  Sleep 1000

  objIE.document.getElementById("ctl00_c_FS_FF").Click
End Sub

当我单击表单的提交按钮时,ctl00_c_CtWNW_ddlTo-suggest文本框变为空并出现错误。

4

1 回答 1

0

您收到该错误是因为您没有给它足够的时间来验证FROMTO下拉列表的名称(是 - 不是文本框)

试试这个代码(尝试和测试)。我也在使用 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 SecondsSleep 5000checkValidation();

方式 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
于 2012-09-27T05:06:43.400 回答