0

I am attempting to navigate to a specific page on a website. The first time I open the page, it asks for a username and password, and every other time after that, it remembers me and is already logged in. I know how to open a browser, how to navigate to specific page, and how to input the username and password to the correct places, all using VBA. I don't want to try and send a username and password when I am already logged in, because those tags wont even exist; the boxes to input those things aren't there. What I need to know is, is there a way to look for a specific tag to see if it is on the webpage in a VBA macro?

Edit: So, I've solved my problem by simply using "On Error Resume Next," so if it hits an error trying to input information to a element that doesn't exist, it'll just jump over it to the page navigation line. It may be a bit hacky, and I'll keep looking for a more elegant solution, but for now, it works. Thank you to all who replied!

3
  • can you post the HTML source for the tag? Commented Nov 4, 2011 at 15:22
  • 1
    If the element has an Id attribute then you can use set el = doc.getElementById("theId") assuming doc is a reference to the currently-loaded document. el will be Nothing if the tag is not there. Commented Nov 4, 2011 at 16:08
  • This code is what works for submitting the user name and passowrd IE.document.Forms(0).all("Login").Value = "generic_login" IE.document.Forms(0).all("Password").Value = "generic_password" IE.document.Forms(0).submit So can i presume that "Login" and "Password" are the tag names? If not, any help you can give is greatly appreciated; I'm pretty new to anything HTML, and completely new to combining VBA and web pages. Commented Nov 4, 2011 at 19:14

2 Answers 2

1

If you have the full HTML loaded in a string variable you can do something like this:

Dim HasLogin As Boolean, HTMLSource As String

HasLogin = (InStr(HTMLSource, "<input name=""login"" id=""username"">") > 0)
Sign up to request clarification or add additional context in comments.

Comments

1

Set up a loop for each input field in the HTML document and check the name like below.

Public Function isLoginScreen(IE As Object) As Boolean
  Set objCollection = IE.document.getElementsByTagName("input")
  Dim i As Long, b As Boolean
  b=False
  i = 0
  While i < objCollection.Length
    If objCollection(i).Name = "Login" Or objCollection(i).Name = "Password" Then 
        ' You found your element
        b=True
        Exit Function
    End If
    i = i + 1
  Wend
End Function

You can see more here: http://www.excely.com/excel-vba/ie-automation.shtml

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.