Results 1 to 6 of 6

Thread: [RESOLVED] Log in a webpage via vb application

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2012
    Posts
    75

    Resolved [RESOLVED] Log in a webpage via vb application

    Hi!

    Here is my code that should fill in "user" and "password" fields on a webpage ("http://forgarden.ee/tellimus") and then click the button "submit" in webbrowser. But it doesn't work!
    It gives me error:Object reference not set to an instance of an object.

    Source code from webpage user and pass fields and button:

    HTML Code:
    ...
    <li>
                                <label for="email" class="required"><em>*</em>E-posti aadress</label>
                                <div class="input-box">
                                    <input name="login[username]" value="" id="email" class="input-text required-entry validate-email" title="E-posti aadress" type="text">
                                </div>
                            </li>
                            <li>
                                <label for="pass" class="required"><em>*</em>Salasõna</label>
                                <div class="input-box">
                                    <input name="login[password]" class="input-text required-entry validate-password" id="pass" title="Salasõna" type="password">
                                </div>
                            </li>
                        </ul>
                        <p class="required">* Nõutud väljad</p>
                    </div>
                </div>
            </div>
            <div class="col2-set">
                <div class="col-1 new-users">
                    <div class="buttons-set">
                    </div>
                </div>
                <div class="col-2 registered-users">
                    <div class="buttons-set">
                        <a href="http://forgarden.ee/tellimus/index.php/customer/account/forgotpassword/" class="f-left">Salasõna läks meelest?</a>
                        <button type="submit" class="button" title="Logi sisse" name="send" id="send2"><span><span>Logi sisse</span></span></button>
                    </div>
                </div>
    
    ...

    And here is my VB code:

    Code:
    Private Sub WebBrowser_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser.DocumentCompleted
    
    
            
            'Dim ForgardenURL As String
            'Dim Pass As String
            'Dim User As String
    
    
            'User = My.Settings.ForgardenUser
            'Pass = My.Settings.ForgardenPass
            'ForgardenURL = My.Settings.ForgardenURL
    
    
            'WebBrowser.Document.GetElementById("login[username]").SetAttribute("value", User)
            'WebBrowser.Document.GetElementById("login[password]").SetAttribute("value", Pass)
                
    
            'WebBrowser.Document.GetElementById("send").InvokeMember("click")
    
            
    
    
        End Sub

    What has gone wrong here?

  2. #2
    Member
    Join Date
    Oct 2009
    Posts
    60

    Re: Log in a webpage via vb application

    Try this

    Code:
    Private Sub WebBrowser_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser.DocumentCompleted
    
    
            
            'Dim ForgardenURL As String
            'Dim Pass As String
            'Dim User As String
    
    
            'User = My.Settings.ForgardenUser
            'Pass = My.Settings.ForgardenPass
            'ForgardenURL = My.Settings.ForgardenURL
    
    
            'WebBrowser.Document.GetElementById("email").SetAttribute("value", Your login Email)
            'WebBrowser.Document.GetElementById("pass").SetAttribute("value", Your Password)
                
    
            'WebBrowser.Document.GetElementById("send").InvokeMember("click")
    
            
    
    
        End Sub

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2012
    Posts
    75

    Re: Log in a webpage via vb application

    It still gives the error like in the first post.

    How could I write a code that fills the fields and clicks login button with HttpWebRequest POST Method? Never used it, maybe it'll work?

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2012
    Posts
    75

    Re: Log in a webpage via vb application

    Flaming hell...that was a braincracker...

    I tried all the tricks and tips I could get my hands on...

    Ended up with only one solution(that works):
    Code:
    Private Sub WebBrowser_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser.DocumentCompleted
    
    
    
            Dim Username = WebBrowser.Document.All("login[username]") 'Searches for username field
            Dim Password = WebBrowser.Document.All("login[password]") 'Searches for password field
    
            Dim pass As String
            Dim user As String
    
            user = My.Settings.ForgardenUser  'doesn't work, think it doesn't convert "@" to string or smth!!!!
            pass = My.Settings.ForgardenPass
    
            If Username <> Nothing And Password <> Nothing Then
                
                Username.InnerText = "[email protected]" 'set the user manually
                Password.InnerText = pass
    
                SendKeys.Send("{ENTER}")
                'hits ENTER to log in instead of click via code(no code worked)
    
            End If
    
    
            
    
    
        End Sub
    Could somebody explain to me:
    * why I couldn't get invokemember("click") to work
    * why an e-mail address stored in my. settings doesn't convert to string and populate username field?

    Is there a better way to write such code?

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Log in a webpage via vb application

    * why I couldn't get invokemember("click") to work
    * why an e-mail address stored in my. settings doesn't convert to string and populate username field?
    It doesn't always. Sometimes the relevant member is not called "click". Sometimes the button just doesn't accept any Invoke.

    You went looking for tags by ID and used its name, for a start. Second, the attribute is not necessarily "value" and, by the same token, "value" is not necessarily an attribute.

    All webpage manipulation requires a degree of trial and error and results are not guaranteed even then. You have to remember that the webpage coder has done nothing to help you. The intention is that you use a webbrowser and mouse to navigate the site and everything is slanted that way. Any degree of 'automation' you are able to achieve, where there's no API expressly created for the purpose, is all bonus.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Aug 2012
    Posts
    75

    Re: Log in a webpage via vb application

    Quote Originally Posted by lkallas View Post
    Code:
    user = My.Settings.ForgardenUser  'doesn't work, think it doesn't convert "@" to string or smth!!!!
    Could somebody explain to me:
    * why an e-mail address stored in my. settings doesn't convert to string and populate username field?
    Answer to my own question is that when I changed the setting name from ForgardenUser to User then it started to work. Go figure!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width