HTML value references in VBA code

ajetrumpet

Banned
Local time
Yesterday, 21:14
Joined
Jun 22, 2007
Messages
5,627
I have written this function to automate a Craig's List login:
Code:
Function Login()

On Error Resume Next

Dim ie As Object
Dim document, element

Set ie = CreateObject("internetexplorer.application")

  ie.navigate "https://accounts.craigslist.org/"

  ie.Visible = True

  While ie.busy
    DoEvents
  Wend

ie.document.all("inputEmailHandle").Value = "myemail"
ie.document.all("inputPassword").Value = "mypassword"
[COLOR="Red"]ie.document.all("submit").Click[/COLOR]

  While ie.busy
    DoEvents
  Wend

End Function
The function works fine without incident, but the submit (log in) button is never clicked through the code. Here is relevant portion of the page's HTML through google's Chrome Browser:
Code:
      <td align="right"><b>Email / Handle:</b></td> 
      <td><input type="text" name="inputEmailHandle" value="" size="40" maxlength="64" tabindex="1"></td> 
    </tr> 
    <tr> 
      <td align="right"><b>Password:</b></td> 
      <td><input type="password" name="inputPassword" size="10" maxlength="40" tabindex="1" AUTOCOMPLETE="off"></td> 
    </tr> 
    
    <tr> 
      <td> </td> 
      <td>[COLOR="Red"]<input type="submit" value="Log In" tabindex="1"><font size="-1"[/COLOR]
The code in RED is where I think the name of the button should be. As you see in the code, the Email and Password text boxes are named inputEmailHandle and inputPassword, respectively. As you can also see, there is no "NAME" property for the "submit" button element. Am I missing something? How do I get the name of the button? It seems to be missing from the code. Am I correct in this?

The bottom line here is that I am completely automating this process if I can get this one simple problem fixed. Thanks for any help!


addition

As a test, I ran this loop inside the procedure:
Code:
For Each element In ie.document.all
  If element.InputType = "submit" Then
    Debug.Print element.Name
  End If
Next element
Nothing printed in the immediate window. Not even an empty line. So, this makes me think that the button has no name. If so, how do I click it? Thank you!
 
Last edited:
Yeah, the HTML definition of the button shows that it doesn't have a name,
<input type="submit" value="Log In" tabindex="1">

But for a simple name element, you'd be laughing. e.g.
<input name="ClickMe" type="submit" value="Log In" tabindex="1">

You could try as you did enumerating for the correct object.
But you could more reasonably just try submitting the form itself.
Instead of
ie.document.all("submit").Click
having
ie.document.Forms(0).Submit

Cheers.
 
Thank you Purvis. Worked a treat (as they say in, UK?) :)
 
I was curious about this, so I gave it a try. Think I got it.

Dim strName As String, strValue As String, strType As String
For Each element In ie.document.all
Err.Clear
On Error Resume Next
strType = Trim(element.Type & "")
If Err.Number <> 0 Then strType = ""
On Error GoTo 0
If strType = "submit" Then
strName = Trim(element.name & "")
strValue = Trim(element.Value & "'")
If InStr(UCase(strValue), "LOG IN") > 0 Then
element.Click
End If
End If
Next element
 
Oh, sorry, I didn't see the other posts.
 
Code:
Function Login()

Dim ie As Object, txtusername , txtpassword 

txtusername = "my email"
txtpassword = "my password"

Set ie = CreateObject("internetexplorer.application")

  ie.navigate "https://accounts.craigslist.org/"

  ie.Visible = True

  While ie.busy
    DoEvents
  Wend

[COLOR="Red"]ie.document.all("inputEmailHandle").Value = txtusername[/COLOR]
ie.document.all("inputPassword").Value = txtpassword
ie.document.Forms(0).submit

  While ie.busy
    DoEvents
  Wend

Set ie = Nothing

End Function
I run this code to automatically in one of my applications to login to Craig's List. My question is...about every 3rd or 4th time I run this function from my application I get the following message (line in red gets highlighted):
Error: Object Variable or With Variable Not Set
I am only opening one instance and one tab of IE, and the temp files are checked to empty when the browser is closed. I close the browser window everytime I rerun the function. Can anyone tell me why I am getting this annoying message!? Any ideas on this? (The code is obviously correct)
 
Does anyone have suggestions for a forum i can go to in order to get this question answered, or to at least shed some light on it? Thanks!
 

Users who are viewing this thread

Back
Top Bottom