gpt4 book ai didi

vba - 在 vba 中提交 Web 表单时输入文本框值为空

转载 作者:行者123 更新时间:2023-12-04 21:06:55 26 4
gpt4 key购买 nike

我正在尝试通过在表单输入文本框中提供所有值来提交网络表单,但是当我从 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文本框变空并出现错误。

最佳答案

您收到该错误是因为您没有给它足够的时间来验证 FROM 的名称。和 TO下拉菜单(是 - 不是文本框)

试试这个代码(尝试和测试)。我也在使用 IE 的后期绑定(bind)。根据您的代码进行更改。
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) 快通过 15 Seconds因为我们没有使用 Sleep 5000用于验证。这不需要您验证下拉菜单。它的作用是绕过 checkValidation();在提交按钮的 ONCLICK 事件中执行的 javascript。

方式 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

关于vba - 在 vba 中提交 Web 表单时输入文本框值为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12611242/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com