0

I am just starting out with VBA and am trying to write a script that does the following interactions with a website:

  1. Login on website

  2. In subsequent webpage, fill in first name and last name from an excel spreadsheet and click "Submit"

  3. In the subsequent webpage, copy an id number that is generated

I have managed to get my code to login on the website through getElementsById since the website's elements do not have any Name. However, excel throws back a Type Mismatch error for my subsequent codes to interact with subsequent webpages.

Here are the HTML script for the firstname, lastname, submit fields in the subsequent webpage:

First Name field:

<input class="tfield-def-edit form-control" id="tfield-def-edit_12803" type="text">

Last Name field:

<input class="tfield-def-edit form-control" id="tfield-def-edit_12804" type="text">

Submit button:

<button class="btn btn-block btn-success" id="btn-submit-registration" type="button">Submit</button>

Here are my VBA codes:

Sub getid()

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
URL = "https://example.website"

IE.Navigate2 URL 'load webpage

Do While IE.readyState <> 4 Or _
   IE.Busy = True
   DoEvents
Loop

'login using password
Set login = IE.document.getElementById("passkey")
login.Value = "123456"
Set loginbutton = IE.document.getElementById("login")
loginbutton.Click

-------------------------------CODE ABOVE WORKS-----------------------------
------------------------------ERRORS OCCUR BELOW----------------------------

While IE.Busy
    DoEvents
Wend

Dim i As Integer
Dim LastRow as Long

With Worksheets("Sheet1")

    Set LastRow = .Range("A" & .Rows.Count).End(xlUp).Rows

    For i = 1 To LastRow

        Set firstname_field = IE.document.getElementById("tfield-def-edit_12803") 'error most probably occur here
        Set lastname_field = IE.document.getElementById("tfield-def-edit_12804")
        Set submitbutton = IE.document.getElementById("btn-submit-registration")

        firstname_field.Value = .Range("B" & i).Value
        lastname_field.Value = .Range("C" & i).Value
        submitbutton.Click

        While IE.Busy
            DoEvents
        Wend

        Set newregistrationbutton = IE.document.getElementByClassName("btn btn-default")
        Set codeid = IE.document.getElementsByClassName("col-xs-6 col-xs-offset-3")
        .Range(D & i).Value = codeid(0).textContent
        newregistrationbutton.Click

    Next i
End With

End Sub

My best guess is that the first name and last name fields are text elements and cannot be Set with a value. When I tried removing Set for all text elements, VBA throws me an error "Object Required".

I am also trying to copy an id that is generated in the subsequent page once the first and last name page runs successfully. I am unable to test the last 4 lines of this code since the preceding codes are not working.

I've been spending days on this and will really appreciate if all of you may shed some light on this.

Tarek.Eladly
  • 730
  • 12
  • 24
Adr.T
  • 23
  • 1
  • 6
  • Have you searched this site and others? **There are literally *hundreds* of examples of how to do this.** Also **if you haven't bothered testing the code then noone else is likely to bother either.** Please see [mcve] as well as [help/on-topic] and the [tour]. Also some [tips on asking a question here](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/). – ashleedawg Mar 12 '18 at 07:37
  • 1
    Would you please share the actual error and the line on which you are getting the error thrown. I would assume the `login` is also of type `input` and since you say that code is working it would lead me to believe there is something else going wrong. – pstrjds Mar 12 '18 at 07:39
  • @ashleedawg you need to read my post carefully before commenting... I have TESTED THESE CODES FOR DAYS but can't seem to solve this. I have also spent at least 24 hours of non-stop searching on this site and others but I cant solve this issue. It is ONLY THE LAST FEW LINES of code that I was UNABLE to test, NOT HAVEN'T BOTHERED since the preceding codes let to errors. Thank you for commenting nonetheless. – Adr.T Mar 12 '18 at 07:46
  • @Adr.T - really? I see: **`I have only coded this from scratch AND HAVE NOT TESTED IT yet (since my codes for the previous page kept throwing errors).`** – ashleedawg Mar 12 '18 at 07:48
  • 1
    Duplicate of : https://stackoverflow.com/questions/48919491/login-to-a-website-using-vba (but I can't flag it since I already voted) – ashleedawg Mar 12 '18 at 07:49
  • @ashleedawg please see my edited post, my previous one may have been confusing for you :) – Adr.T Mar 12 '18 at 07:49
  • please send me the url to find out the field. – Tarek.Eladly Mar 12 '18 at 07:52
  • @pstrjds I am getting "Runtime Error 13: Type Mismatch" on the lines: `Set firstname_field = IE.document.getElementById("tfield-def-edit_12803")` – Adr.T Mar 12 '18 at 07:52
  • Possible duplicate of [Login to a website using VBA](https://stackoverflow.com/questions/48919491/login-to-a-website-using-vba) – Pᴇʜ Mar 12 '18 at 07:52
  • @Adr.T give me the url i think i can help – Tarek.Eladly Mar 12 '18 at 07:55
  • @Pᴇʜ I have followed these questions but I always seem to get the "Runtime Error 13: Type Mismatch" although my codes are similar. Will really appreciate if someone can help me point out where I've coded wrongly. Thank you! – Adr.T Mar 12 '18 at 07:56
  • @Tarek.Eladly I would love to but its an internal website and you will need the login for that but I'm unable to provide it. Thank you for your help! – Adr.T Mar 12 '18 at 07:57
  • you code work fine the problem with the generated html that you don't reference it that's all – Tarek.Eladly Mar 12 '18 at 07:58
  • ok look in the Javascript of the page and find out where the fields is generated and get the reference from there. – Tarek.Eladly Mar 12 '18 at 08:00
  • or better get the form itself and then get the child element one by one – Tarek.Eladly Mar 12 '18 at 08:01
  • do you run it all to the end or just a part of the application – Tarek.Eladly Mar 12 '18 at 08:45

1 Answers1

0

your code work fine i just test it on a dummy pages and it is good, what i think is that the page with the fields ("tfield-def-edit_12803","tfield-def-edit_12804") that you are trying to change its values always change so it is always giving you

Run-time error '13': Type Mismatch

so all you have to do it to find out where it is created in the page then get the ID from there, I hope this help.

Or maybe the fields is generated after the document is ready try to wait for 5 seconds and then get the data use this to wait

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub test()
Dim i As Long

For i = 1 To 10
    Debug.Print Now()
    Sleep 5000    'wait 5 seconds
Next i
End Sub 
Tarek.Eladly
  • 730
  • 12
  • 24
  • Hi Tarek thanks for your troubleshooting. May I ask how can I discover if the fields are always changing? I have tried to access the website multiple times but the fields ("tfield-def-edit_12803","tfield-def-edit_12804") always have the same Id – Adr.T Mar 12 '18 at 08:09
  • ok try to wait for 5 seconds then get the fields maybe the fields is generated after the document is ready. – Tarek.Eladly Mar 12 '18 at 08:11
  • That works perfectly!! I'm now able to proceed into the next webpage but am unable to copy and paste the code id generated. I received the following error message: Run-time error '438': Object doesn't support this property or method – Adr.T Mar 12 '18 at 08:22
  • in which line ? – Tarek.Eladly Mar 12 '18 at 08:23
  • The number that I need to copy and paste is a standalone number and is not labelled as an ID, name etc. How can I direct my script to copy that particular nnumber? – Adr.T Mar 12 '18 at 08:51
  • just get the container parent then start parsing the text to isolate the data give me any html with any unique ID so i can send you an example – Tarek.Eladly Mar 12 '18 at 08:55