Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am writing a program to fill in a form on a webpage and then submit it. However, I can't get the form to submit.

I have included a link to the website.

The button is called 'Get Instant Quotes' but I can't seem to be able to reference it at all.

My code so far

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 = "[email protected]"
'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
share|improve this question
2  
Show us some code. – ron tornambe yesterday
The sourcecode near the button is div class="row inset"> <a href="javascript:void(0);" class="button orange getQuotes large">Get Instant Quotes</a> – user2333507 yesterday
It also says this in the source code <div class="contactForm quoteForm"> <div class="contactFormInner"> <div class="offer"><img src="/img/from5.png" width="136" height="136" alt="From £5 a month" border="0" /></div> <h3 class="withoffer">Three Step Life Insurance Quote Service</h3> – user2333507 yesterday

1 Answer

Since IE supports getELementsByClassName, you can reference the anchor element (Get Instant Quotes) as follows (and should be able to click it - although I haven't tried it):

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

Let me know if this works. If not, I'll setup a test site and we can take it from there.

EDIT

Here's a test I setup (using MS-Access 2007 VBA) that merely causes the form to be submitted. Since I have not filled in any fields, the site shows the required fields that are empty. Try it for yourself:

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 Solution

This solution uses getElementsByTagName, which is supported by IE8. It is not terribly elegant, but works.

Set IE = CreateObject("InternetExplorer.Application")
IE.navigate "https://www.yourwealth.co.uk/insurance/life-insurance-quotes#46870bb5/"
IE.Visible = True
aElems = IE.Document.getElementsByTagName("a")
For elem = 0 To IE.Document.getElementsByTagName("a").Length - 1
  If IE.Document.getElementsByTagName("a")(elem).className = "button orange getQuotes large" Then
    IE.Document.getElementsByTagName("a")(elem).Click
    Exit For
  End If
Next
share|improve this answer
Hi, thanks for the reply. I tried this but it didn't like it. It wouldn't let me add the [0] part either. I tried IE.Document.getElementsByClassName("button orange getQuotes large").Click but I get the error message This object doen't support this property or method. – user2333507 yesterday
Did you get an error. Just to be sure it is finding the element, please try: alert(IE.Document.getElementsByClassName('button orange getQuotes large')[0].length) and see if a "1" is displayed. – ron tornambe yesterday
Forget my last comment and just replace [0] with (0) and give it another try. – ron tornambe yesterday
Hi, I replaced the [0] with (0) but I get the same error message. 'This object doesn't support this property or method' – user2333507 19 hours ago
I'm not sure if it is a button, but a JavaScript form that needs submitting. – user2333507 18 hours ago
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.