PDA

View Full Version : Not able to access elements on a web page



musicgold
08-02-2011, 02:04 PM
Hi,

I am trying to use VBA to access the login form on this page
http://forum.wordreference.com/newthread.php?do=newthread&f=6 (http://forum.wordreference.com/newthread.php?do=newthread&f=6)

For some reason I am not able to fill the username and password textboxes. The watch window of VBA shows me the values of the elements are changing but I am not able to see any text on the webpage.

Please see my code below.


Set ObjIE = CreateObject("InternetExplorer.Application")

With ObjIE

.Visible = 1
.navigate "http://forum.wordreference.com/newthread.php?do=newthread&f=6"


Do While .Busy: DoEvents: Loop
Do While .readyState <> 4: DoEvents: Loop


Set htmlDoc = .document
htmlDoc.getElementById("vb_login_username").Value = "ABC"
htmlDoc.getElementById("vb_login_password").Value = "DEF"
htmlDoc.forms(2).submit
End with



Thanks.

Kenneth Hobs
08-02-2011, 07:25 PM
Sub t()
Dim ObjIE As Object, htmlDoc As Object
Set ObjIE = CreateObject("InternetExplorer.Application")

With ObjIE

.Visible = 1
.navigate "http://forum.wordreference.com/newthread.php?do=newthread&f=6"


Do While .Busy: DoEvents: Loop
Do While .readyState <> 4: DoEvents: Loop


Set htmlDoc = .document
htmlDoc.getElementByID("navbar_username").Value = "ABC"
htmlDoc.getElementByID("navbar_password").Value = "DEF"
htmlDoc.forms(1).submit
End With

End Sub

musicgold
08-03-2011, 05:04 AM
Thanks Kenneth.

I knew about the login box at the top right, that you are using to login into the website.

However, I was more interested in finding out why I am not able to access the main login form. Why can't I access those textboxes?

Thanks.

Kenneth Hobs
08-03-2011, 08:23 AM
Use forms(1). I am not sure why the forum is changing the indentation.
Sub LoginWordReference1()
Dim ObjIE As Object, htmlDoc As Object
Set ObjIE = CreateObject("InternetExplorer.Application")
With ObjIE
.Visible = 1
.navigate "http://forum.wordreference.com/newthread.php?do=newthread&f=6"
Do While .Busy: DoEvents: Loop
Do While .readyState <> 4: DoEvents: Loop
Set htmlDoc = .document
htmlDoc.getElementByID("vb_login_username").Value = "ABC"
htmlDoc.getElementByID("vb_login_password").Value = "DEF"
htmlDoc.forms(1).submit
End With
End Sub

musicgold
08-03-2011, 08:34 AM
Thanks Kenneth.

No, this is not working.

If you single step through your code, you will notice that the getelementbyID statements are not able to put those values into the username and password boxes.

Kenneth Hobs
08-03-2011, 11:58 AM
I seldom post code that does not work for me.

frank_m
08-03-2011, 04:28 PM
Kenneths code fills in both textboxes just fine on my machine. (in excel 2003 and 2007)

musicgold
08-05-2011, 06:53 PM
Thanks folks.

This is strange. I tried Kenneth's code both on my work machine and home machine to no avail. Both machines have Windows XP and Office 2003.

I am attaching a screenshot from my home machine. You can see in the screeshot that the text boxes on the webpage are empty even after the code execution is past the getelementbyID statements. Similar code works fine for other websites on both machines. I don't know what is going on.

Thanks.

Kenneth Hobs
08-05-2011, 07:41 PM
I am not sure if I have 2003 anymore. I use 2010. The more important version is MSIE. I use version 9.0.8112.16421. I will see if my old computer has the old versions tomorrow.