使用VBA提交javascript表单

使用VBA提交javascript表单,javascript,forms,vba,Javascript,Forms,Vba,我正在写一个程序,在网页上填写表格,然后提交。然而,我无法提交表格 我已经包括了一个链接到 按钮名为“获取即时报价”,但我似乎根本无法引用它 到目前为止我的代码 Application.Calculation = xlCalculationAutomatic 'Determine Number of Quotes If Range("Start").Offset(2, 0) = "" Then TotalQuotes = 1 Else TotalQuotes = Range("S

我正在写一个程序,在网页上填写表格,然后提交。然而,我无法提交表格

我已经包括了一个链接到

按钮名为“获取即时报价”,但我似乎根本无法引用它

到目前为止我的代码

Application.Calculation = xlCalculationAutomatic
'Determine Number of Quotes
If Range("Start").Offset(2, 0) = "" Then
    TotalQuotes = 1
Else
    TotalQuotes = Range("Start").End(xlDown).Row - 4
End If
i = 4
T = i - 3
Application.StatusBar = "Getting Quote " & T & " out of " & TotalQuotes
Do Until Worksheets("Front").Cells(i, 1) = ""
Website = Worksheets("Front").Range("A1")
Set IE = CreateObject("InternetExplorer.Application")
IE.navigate Website

IE.Visible = True

While IE.Busy
   DoEvents  'wait until IE is done loading page.
Wend
While IE.readyState <> 4
DoEvents
Wend
'Type of Product
'Life Only
Worksheets("Front").Select
If Mid(Cells(i, 2), 2, 1) = "T" Then
    CIC_IND = 0
    CIC_Type = "guaranteed"
Else
    CIC_IND = 1
    CIC_Type = Cells(i, 7)
    If CIC_Type = "G" Then
        CIC_Type = "guaranteed"
    Else
        CIC_Type = "(reviewabl"
    End If
    IE.Document.getElementById("IncludeCriticalIllnessPolicyYes").Click
End If
'Name
IE.Document.getElementById("strFirstName").Value = "Andy Williams"
'Address
IE.Document.getElementById("address").Value = "10 Jones Street"
'City
IE.Document.getElementById("city").Value = "leicester"
'Post Code
IE.Document.getElementById("strPostcode").Value = "LE1 2GH"
'Phone Number
IE.Document.getElementById("strPhoneNumber").Value = "01245337235"
'Email
IE.Document.getElementById("strEmail").Value = "Andy.williams@hotmail.com"
'Single or Joint
If Left(Cells(i, 3), 1) = "J" Then
    IE.Document.getElementById("strData3").Value = "YOU_PARTNER"
Else
    IE.Document.getElementById("strData3").Value = "YOU"
End If
'Level or Decreasing
If Left(Cells(i, 2), 1) = "D" Then
    IE.Document.getElementById("strData4").Value = "MORTGAGE_PROTECTION"
Else
    IE.Document.getElementById("strData4").Value = "PL_COVER"
End If
'Sum Assured
SumAssured = Worksheets("Front").Cells(i, 6)
IE.Document.getElementById("strData2").Value = SumAssured
'Policy Term
Policy_Term = Worksheets("Front").Cells(i, 5)
IE.Document.getElementById("strData5").Value = Policy_Term
'Date of Birth
DOB_Year = Right(Cells(2, 1), 4) - Cells(i, 4)
DOB_Month = Mid(Cells(2, 1), 4, 2)
DOB_Day = Left(Cells(2, 1), 2)
IE.Document.getElementById("strDOBDay").Value = DOB_Day
IE.Document.getElementById("strDOBMonth").Value = DOB_Month
IE.Document.getElementById("strDOBYear").Value = DOB_Year
'Gender
IE.Document.getElementById("strGender").Value = "MALE"
'Smoker Status
If Right(Cells(i, 3), 1) = "M" Then
    IE.Document.getElementById("StrSmoker").Value = "YES"
Else
    IE.Document.getElementById("StrSmoker").Value = "NO"
End If
'If Left(Cells(i, 3), 1) = "J" Then
 '   Joint_Ind = 1
  '  IE.document.GetElementById("SecondApplicantTitle").Value = "2"
   ' IE.document.GetElementById("SecondApplicantForename").Value = "Andrea"
    'IE.document.GetElementById("secondApplicantSurname").Value = "Williams"
    'Date of Birth
    'DOB_Year = Right(Cells(2, 1), 4) - Cells(i, 4)
    'DOB_Month = Mid(Cells(2, 1), 4, 2)
'    DOB_Month = 3
 '   DOB_Day = Left(Cells(2, 1), 2)
  '  DOB = DOB_Day & "/" & DOB_Month & "/" & DOB_Year
   ' IE.document.GetElementById("SecondApplicantDateOfBirth").Value = DOB
    'Smoker Status
    'If Right(Cells(i, 3), 1) = "M" Then
     '   IE.document.GetElementById("SecondApplicantSmokerYes").Click
    'Else
     '   IE.document.GetElementById("SecondApplicantSmokerNo").Click
    'End If
'Else
 '   Joint_Ind = 0
'End If
Application.Calculation=xlCalculationAutomatic
'确定引号的数量
如果范围(“开始”)。偏移量(2,0)=“那么
TotalQuotes=1
其他的
TotalQuotes=范围(“开始”).End(xlDown).第4行
如果结束
i=4
T=i-3
Application.StatusBar=“Getting Quote”&T&“out”&TotalQuotes
直到工作表(“前”)单元格(i,1)=“
网站=工作表(“正面”)。范围(“A1”)
设置IE=CreateObject(“InternetExplorer.Application”)
浏览网站
可见=真实
趁我忙
DoEvents'等待IE完成加载页面。
温德
而IE.readyState 4
多芬特
温德
“产品类型”
“只有生命
工作表(“正面”)。选择
如果Mid(单元格(i,2),2,1)=“T”,则
CIC_IND=0
CIC_Type=“保证”
其他的
CIC_IND=1
CIC_类型=单元(i,7)
如果CIC_Type=“G”,则
CIC_Type=“保证”
其他的
CIC_Type=“(reviewabl)”
如果结束
IE.Document.getElementById(“IncludeCriticalIllnessPolicyYes”)。单击
如果结束
“名字
IE.Document.getElementById(“strFirstName”).Value=“Andy Williams”
“地址
IE.Document.getElementById(“地址”).Value=“琼斯街10号”
“城市
IE.Document.getElementById(“城市”).Value=“莱斯特”
“邮政编码
IE.Document.getElementById(“strPostcode”).Value=“LE1 2GH”
“电话号码
IE.Document.getElementById(“strPhoneNumber”).Value=“01245337235”
“电子邮件
IE.Document.getElementById(“strEmail”).Value=“Andy。williams@hotmail.com"
“单一的还是联合的
如果左(单元格(i,3),1)=“J”,则
IE.Document.getElementById(“strData3”).Value=“您的合作伙伴”
其他的
IE.Document.getElementById(“strData3”).Value=“您”
如果结束
“水平还是下降
如果左(单元格(i,2),1)=“D”,则
IE.Document.getElementById(“strData4”).Value=“抵押权保护”
其他的
IE.Document.getElementById(“strData4”).Value=“PL\U封面”
如果结束
“保证金额
SUMSured=工作表(“前”)单元格(i,6)
IE.Document.getElementById(“strData2”).Value=summated
“政策术语
政策术语=工作表(“前”)单元格(i,5)
IE.Document.getElementById(“strData5”).Value=Policy\u术语
“出生日期
DOB_Year=右侧(单元格(2,1,4)-单元格(i,4)
月初=月中(单元格(2,1,4,2)
出生日期=左侧(单元格(2,1,2)
IE.Document.getElementById(“strDOBDay”).Value=DOB\u Day
IE.Document.getElementById(“strDOBMonth”).Value=DOB\u月
IE.Document.getElementById(“标准对象年”).Value=DOB\u年
“性别
IE.Document.getElementById(“strGender”).Value=“男性”
“吸烟者身份
如果正确(单元格(i,3),1)=“M”,则
IE.Document.getElementById(“StrSmoker”).Value=“是”
其他的
IE.Document.getElementById(“StrSmoker”).Value=“否”
如果结束
'如果左(单元格(i,3),1)=“J”,则
'接头=1
'IE.document.GetElementById(“第二个应用程序标题”).Value=“2”
'IE.document.GetElementById(“SecondApplicationForeName”).Value=“Andrea”
'IE.document.GetElementById(“第二个应用程序姓氏”).Value=“Williams”
“出生日期
'出生年份=右侧(单元格(2,1,4)-单元格(i,4)
'月龄=月中(单元格(2,1,4,2)
'月龄=3
'出生日期=左侧(单元格(2,1,2)
'DOB=DOB_日&“/”&DOB_月&“/”&DOB年
'IE.document.GetElementById(“第二个应用程序出生日期”)。值=DOB
“吸烟者身份
'如果正确(单元格(i,3),1)=“M”,则
'IE.document.GetElementById(“第二个应用程序Smokeryes”)。单击
”“否则呢
'IE.document.GetElementById(“第二个应用程序SmokerNo”)。单击
"完"
”“否则呢
'Joint_Ind=0
"完"

由于IE支持GetElementsByCassName,您可以按如下方式引用锚元素(获取即时引号)(应该能够单击它-尽管我没有尝试过):

让我知道这是否可行。如果不行,我将设置一个测试站点,我们可以从那里开始

编辑

这里有一个test I设置(使用MS Access 2007 VBA),它只会导致提交表单。由于我没有填写任何字段,因此该站点显示的必填字段为空。请自己尝试:

Set IE = CreateObject("InternetExplorer.Application")
IE.navigate "https://www.yourwealth.co.uk/insurance/life-insurance-quotes#46870bb5/"
IE.Visible = True
IE.Document.getElementsByClassName("button orange getQuotes large")(0).Click
IE8解决方案

此解决方案使用IE8支持的getElementsByTagName

Set IE = CreateObject("InternetExplorer.Application")
IE.navigate "https://www.yourwealth.co.uk/insurance/life-insurance-quotes#46870bb5/"
IE.Visible = True
Dim elems As Object
Set elems = IE.Document.getElementsByTagName("a")
Dim elem As Object
For Each elem In elems
  If elem.className = "button orange getQuotes large" Then
    elem.Click
    Exit For
  End If
Next

按钮附近的源代码是div class=“row inset”>它在源代码“三步人寿保险报价服务”中也有这样的说明嗨,谢谢你的回复。我尝试了这个,但它不喜欢。它也不允许我添加[0]部分。我尝试了IE.Document.getElementsByClassName(“按钮橙色getQuotes large”)。单击,但我收到错误消息此对象不支持此属性或方法。您是否收到错误。为了确保它正在查找元素,请尝试:alert(即.Document.getElementsByClassName('button orange getQuotes large')[0].length)并查看是否显示“1”。忘记我的上一条注释,将[0]替换为(0)请再试一次。您好,我将[0]替换为(0),但收到相同的错误消息。“此对象不支持此属性或方法”我不确定它是否为按钮,但它是一个需要提交的JavaScript表单。
Set IE = CreateObject("InternetExplorer.Application")
IE.navigate "https://www.yourwealth.co.uk/insurance/life-insurance-quotes#46870bb5/"
IE.Visible = True
Dim elems As Object
Set elems = IE.Document.getElementsByTagName("a")
Dim elem As Object
For Each elem In elems
  If elem.className = "button orange getQuotes large" Then
    elem.Click
    Exit For
  End If
Next