0

我正在编写一个程序来在网页上填写表格然后提交。但是,我无法提交表单。

我已经包含了该网站的链接。

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

到目前为止我的代码

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
4

1 回答 1

0

由于 IE 支持 getELementsByClassName,您可以按如下方式引用锚元素(Get Instant Quotes)(并且应该能够单击它 - 虽然我没有尝试过):

IE.Document.getElementsByClassName('button orange getQuotes large')[0].Click

让我知道这个是否奏效。如果没有,我将设置一个测试站点,我们可以从那里获取它。

编辑

这是我设置的一个测试(使用 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
于 2013-04-29T21:36:54.563 回答