简体   繁体   中英

Use VBA/VBSCRIPT to access Textbox embeded in the form tags of Internet Explorer Object

I am using VBA to access a username/password box on a webpage that has a form

 <form name="form1" method="post" action...

I have successfully used the code below to login to several sites that do not have the controls placed in the form tag. However, The following code does not work on this particular webpage, since i am assuming it is because the username/password boxes are in the form tags.

   Set IE = CreateObject("internetexplorer.application")
   IE.Visible = True
   IE.Navigate "websiteurl"

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

   IE.Document.getElementById("username").Value = "username"
   IE.Document.getElementById("pwd").Value = "password"
   IE.Document.all("ButSubmit").Click

I get an error "AUTOMATION ERROR. The interface is unknown". I have not been able to find much info on this type of problem. Would appreciate any help that can be given on this one

As the comments demonstrate, the username and pwd fields might have no ID's defined, as in the general case of form input elements, you can work around with this:

Sub sofIELogin20080792()
  Dim strUrl
  Dim IE, frm
  Set IE = CreateObject("InternetExplorer.Application")

  strUrl = "http://www.stackoverflow.com/users/login"
  IE.Navigate strUrl
  Do While IE.ReadyState <> 4 Or IE.Busy = True
    DoEvents
  Loop
  '
  ' get the form object to input username and password:
  '
  Set frm = IE.Document.getElementsByName("form1")(0)
  frm.elements("username").Value = "username"
  frm.elements("pwd").Value = "password"
  frm.submit
  '
  Do While IE.ReadyState <> 4 Or IE.Busy = True
    DoEvents
  Loop
'
  IE.Visible = True
'
  Set frm = Nothing
  Set IE = Nothing
'
  MsgBox "User login has been tried."
'
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM