Link to home
Start Free TrialLog in
Avatar of adwooley2
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?


Avatar of Hillwaaa
Hillwaaa
Flag of Australia image

Hi adwooley2,

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
Avatar of adwooley2
adwooley2

ASKER

Thanks for the quick reply, Hillwaa.  I'm going to hold out and see what else might be out there.
Avatar of Éric Moreau
Hi adwooley2,

which site is it? depending on the authentication form, different method are required.

Cheers!
Hi.  The site is http://asnet.autoserver.co.jp/asnet20e/top/logon.asp.  Is there some way of doing it using IHTML objects?
Regards,
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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").Value = "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?

have you tried (in which oIE is you browser object):

oIE.Document.All("USER").value="xxxx"
oIE.Document.All("PASSWORD").value="xxxxx"
oIE.Document.Forms(0).Submit
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.HtmlDocument

'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.GetElementsByTagName("input")
            If element.Name = "in_user_id" Then
                user = element.DomElement
            ElseIf element.Name = "in_password" Then
                password = element.DomElement
            ElseIf element.GetAttribute("value") = "Login" Then
                login = element.DomElement
            End If
        Next element

        user.value = "User101"
        password.value = "Password101"

        login.click()
    End Sub