adwooley2
asked on
Login to website programmatically with VB.NET
Very tough question. I have a program that scrapes data off of a website that requires username/password to login.
I can navigate to the webpage programatically with no problem, but after that, I would like to post the username and password to the website automatically using VB.NET (this is a Windows app, not ASP).
So, here's what I want to do:
1) Navigate to the website (able to do this)
2) Login with username/password (NEED A SOLUTION HERE)
3) Traverse screens and scrape the data (able to do this)
4) Force a logout (NEED A SOLUTION HERE but not sure if this is possible)
5) Repeat steps 1 thru 4.
Step #2 is the big question. Can anybody help?
I can navigate to the webpage programatically with no problem, but after that, I would like to post the username and password to the website automatically using VB.NET (this is a Windows app, not ASP).
So, here's what I want to do:
1) Navigate to the website (able to do this)
2) Login with username/password (NEED A SOLUTION HERE)
3) Traverse screens and scrape the data (able to do this)
4) Force a logout (NEED A SOLUTION HERE but not sure if this is possible)
5) Repeat steps 1 thru 4.
Step #2 is the big question. Can anybody help?
ASKER
Thanks for the quick reply, Hillwaa. I'm going to hold out and see what else might be out there.
Hi adwooley2,
which site is it? depending on the authentication form, different method are required.
Cheers!
which site is it? depending on the authentication form, different method are required.
Cheers!
ASKER
Hi. The site is http://asnet.autoserver.co.jp/asnet20e/top/logon.asp. Is there some way of doing it using IHTML objects?
Regards,
Regards,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
emoreau,
Thanks for the link.
From that, I focused on Tim Byrne's code as follows:
========================== ========== =
"If you add web browser control you could use the follow to login"
Dim doc As HTMLDocument
Set doc = WebBrowser1.Document
'Login() being the document name
doc.login("USER").Value = "Test"
doc.login("PASSWORD").Valu e = "Test"
doc.login("Submit").Click
========================== ========== =
I don't understand, however, what "Login() being the document name" means exactly. When I try to use this code, it says that login is not a member of HTMLDocument. Can anybody shed some light on this?
Thanks for the link.
From that, I focused on Tim Byrne's code as follows:
==========================
"If you add web browser control you could use the follow to login"
Dim doc As HTMLDocument
Set doc = WebBrowser1.Document
'Login() being the document name
doc.login("USER").Value = "Test"
doc.login("PASSWORD").Valu
doc.login("Submit").Click
==========================
I don't understand, however, what "Login() being the document name" means exactly. When I try to use this code, it says that login is not a member of HTMLDocument. Can anybody shed some light on this?
have you tried (in which oIE is you browser object):
oIE.Document.All("USER").v alue="xxxx "
oIE.Document.All("PASSWORD ").value=" xxxxx"
oIE.Document.Forms(0).Subm it
oIE.Document.All("USER").v
oIE.Document.All("PASSWORD
oIE.Document.Forms(0).Subm
ASKER
emoreau,
Thanks for your reply. I actually figured this out (but will give you the points.
For all those that want to know:
'DECLARE AT THE CLASS LEVEL
Private m_document As System.Windows.Forms.HtmlD ocument
'NAVIGATE BROWSER TO DESIRED SITE HERE
'RUN THIS LOGIN SUB
Private Sub Login()
Dim user As mshtml.HTMLInputElement = Nothing
Dim password As mshtml.HTMLInputElement = Nothing
Dim login As mshtml.HTMLInputElement = Nothing
m_document = Me.WebBrowser1.Document
For Each element As HtmlElement In m_document.GetElementsByTa gName("inp ut")
If element.Name = "in_user_id" Then
user = element.DomElement
ElseIf element.Name = "in_password" Then
password = element.DomElement
ElseIf element.GetAttribute("valu e") = "Login" Then
login = element.DomElement
End If
Next element
user.value = "User101"
password.value = "Password101"
login.click()
End Sub
Thanks for your reply. I actually figured this out (but will give you the points.
For all those that want to know:
'DECLARE AT THE CLASS LEVEL
Private m_document As System.Windows.Forms.HtmlD
'NAVIGATE BROWSER TO DESIRED SITE HERE
'RUN THIS LOGIN SUB
Private Sub Login()
Dim user As mshtml.HTMLInputElement = Nothing
Dim password As mshtml.HTMLInputElement = Nothing
Dim login As mshtml.HTMLInputElement = Nothing
m_document = Me.WebBrowser1.Document
For Each element As HtmlElement In m_document.GetElementsByTa
If element.Name = "in_user_id" Then
user = element.DomElement
ElseIf element.Name = "in_password" Then
password = element.DomElement
ElseIf element.GetAttribute("valu
login = element.DomElement
End If
Next element
user.value = "User101"
password.value = "Password101"
login.click()
End Sub
You could try simulating the keypresses - using SendKeys to "type" the username, then tab to the password field, and type that. To do this you would need to get a reference to the web browser window using FindWindow as well. Let me know if you want more info on how to do this - it's not the easiest thing in the world, so I'll see if there are better solutions first :)
Cheers,
Hillwaaa