Solved

Webbrowser Infinite loop issue in VB.net

Posted on 2010-09-20
5
734 Views
Last Modified: 2012-08-13
Hi,

I am using webbrowser object to open a web page using my winodws application. I am able to open the web page login page. When clicks on login button of that page it is showing processing only. It not getting login.

How can i solve the above issue.Please provide the solution

Thanks
0
Comment
Question by:futurebuilders
  • 3
  • 2
5 Comments
 
LVL 19

Expert Comment

by:Shahan Ayyub
ID: 33727167
Hi!

At which event you are navigating ??
Is this WebBrowser Control or AxWebBrowser Control?

can you post some code ??

because it works pretty well. I have used both controls several times.
0
 

Accepted Solution

by:
futurebuilders earned 0 total points
ID: 33731811
HI,

I am using Webbroser control only. Please find the attached code.

Imports System
Imports System.Windows.Forms
Imports System.Security.Permissions

<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
Public Class Form2
    Inherits Form

    Private m_isLoggedIn As Boolean

    Public Sub New()

        ' Create the form layout. If you are using Visual Studio,
        ' you can replace this code with code generated by the designer.
        InitializeForm()

        Dim obj As New WrapperClass
        Dim str As String

        webBrowser1.Navigate("https://www.worldofwarcraft.com/account/index-trial.html")

    End Sub

    Private Sub webBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles webBrowser1.DocumentCompleted
        If Not m_isLoggedIn Then
            Login(strUserName, strPassoword)
        Else
            ' Process next step
        End If
    End Sub

    Private Sub Login(ByVal userName As String, ByVal password As String)
        Dim accountNameElement As HtmlElement = webBrowser1.Document.GetElementById("accountName")
        If accountNameElement Is Nothing Then
            Throw New NullReferenceException("Cannot find account name input element")
        Else
            accountNameElement.InnerText = userName
        End If

        Dim passwordElement As HtmlElement = webBrowser1.Document.GetElementById("password")
        If passwordElement Is Nothing Then
            Throw New NullReferenceException("Cannot find password input element")
        Else
            passwordElement.InnerText = password
        End If

        m_isLoggedIn = True

    End Sub

    ' Displays the Save dialog box.
    Private Sub saveAsToolStripMenuItem_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles saveAsToolStripMenuItem.Click

        webBrowser1.ShowSaveAsDialog()

    End Sub

    ' Displays the Page Setup dialog box.
    Private Sub pageSetupToolStripMenuItem_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles pageSetupToolStripMenuItem.Click

        webBrowser1.ShowPageSetupDialog()

    End Sub

    ' Displays the Print dialog box.
    Private Sub printToolStripMenuItem_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles printToolStripMenuItem.Click

        webBrowser1.ShowPrintDialog()

    End Sub

    ' Displays the Print Preview dialog box.
    Private Sub printPreviewToolStripMenuItem_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles printPreviewToolStripMenuItem.Click

        webBrowser1.ShowPrintPreviewDialog()

    End Sub

    ' Displays the Properties dialog box.
    Private Sub propertiesToolStripMenuItem_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles propertiesToolStripMenuItem.Click

        webBrowser1.ShowPropertiesDialog()

    End Sub

    ' Selects all the text in the text box when the user clicks it.
    Private Sub toolStripTextBox1_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles toolStripTextBox1.Click

        toolStripTextBox1.SelectAll()

    End Sub

    ' Navigates to the URL in the address box when
    ' the ENTER key is pressed while the ToolStripTextBox has focus.
    Private Sub toolStripTextBox1_KeyDown( _
        ByVal sender As Object, ByVal e As KeyEventArgs) _
        Handles toolStripTextBox1.KeyDown

        If (e.KeyCode = Keys.Enter) Then
            Navigate(toolStripTextBox1.Text)
        End If

    End Sub

    ' Navigates to the URL in the address box when
    ' the Go button is clicked.
    Private Sub goButton_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles goButton.Click

        Navigate(toolStripTextBox1.Text)

    End Sub

    ' Navigates to the given URL if it is valid.
    Private Sub Navigate(ByVal address As String)

        If String.IsNullOrEmpty(address) Then Return
        If address.Equals("about:blank") Then Return
        If Not address.StartsWith("http://") And _
            Not address.StartsWith("https://") Then
            address = "http://" & address
        End If

        Try
            webBrowser1.Navigate(New Uri(address))
        Catch ex As System.UriFormatException
            Return
        End Try

    End Sub

    ' Updates the URL in TextBoxAddress upon navigation.
    Private Sub webBrowser1_Navigated(ByVal sender As Object, _
        ByVal e As WebBrowserNavigatedEventArgs) _
        Handles webBrowser1.Navigated

        toolStripTextBox1.Text = webBrowser1.Url.ToString()

    End Sub

    ' Navigates webBrowser1 to the previous page in the history.
    Private Sub backButton_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles backButton.Click

        webBrowser1.GoBack()

    End Sub

    ' Disables the Back button at the beginning of the navigation history.
    Private Sub webBrowser1_CanGoBackChanged( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles webBrowser1.CanGoBackChanged

        backButton.Enabled = webBrowser1.CanGoBack

    End Sub

    ' Navigates webBrowser1 to the next page in history.
    Private Sub forwardButton_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles forwardButton.Click

        webBrowser1.GoForward()

    End Sub

    ' Disables the Forward button at the end of navigation history.
    Private Sub webBrowser1_CanGoForwardChanged( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles webBrowser1.CanGoForwardChanged

        forwardButton.Enabled = webBrowser1.CanGoForward

    End Sub

    ' Halts the current navigation and any sounds or animations on
    ' the page.
    Private Sub stopButton_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles stopButton.Click

        webBrowser1.Stop()

    End Sub

    ' Reloads the current page.
    Private Sub refreshButton_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles refreshButton.Click

        ' Skip refresh if about:blank is loaded to avoid removing
        ' content specified by the DocumentText property.
        If Not webBrowser1.Url.Equals("about:blank") Then
            webBrowser1.Refresh()
        End If

    End Sub

    ' Navigates webBrowser1 to the home page of the current user.
    Private Sub homeButton_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles homeButton.Click

        webBrowser1.GoHome()

    End Sub

    ' Navigates webBrowser1 to the search page of the current user.
    Private Sub searchButton_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles searchButton.Click

        webBrowser1.GoSearch()

    End Sub

    ' Prints the current document Imports the current print settings.
    Private Sub printButton_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles printButton.Click

        webBrowser1.Print()

    End Sub

    ' Updates the status bar with the current browser status text.
    'Private Sub webBrowser1_StatusTextChanged( _
    '    ByVal sender As Object, ByVal e As EventArgs) _
    '    Handles webBrowser1.StatusTextChanged

    '    toolStripStatusLabel1.Text = webBrowser1.StatusText

    'End Sub

    ' Updates the title bar with the current document title.
    Private Sub webBrowser1_DocumentTitleChanged( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles webBrowser1.DocumentTitleChanged
        ' webBrowser1.Refresh()
        Me.Text = webBrowser1.DocumentTitle

    End Sub

    ' Exits the application.
    Private Sub exitToolStripMenuItem_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles exitToolStripMenuItem.Click

        Application.Exit()

    End Sub

    ' The remaining code in this file provides basic form initialization and
    ' includes a Main method. If you use the Visual Studio designer to create
    ' your form, you can use the designer generated code instead of this code,
    ' but be sure to use the names shown in the variable declarations here.

    Private WithEvents webBrowser1 As WebBrowser

    Private menuStrip1 As MenuStrip
    Private WithEvents fileToolStripMenuItem, saveAsToolStripMenuItem, _
        printToolStripMenuItem, printPreviewToolStripMenuItem, _
        exitToolStripMenuItem, pageSetupToolStripMenuItem, _
        propertiesToolStripMenuItem As ToolStripMenuItem
    Private toolStripSeparator1, toolStripSeparator2 As ToolStripSeparator

    Private toolStrip1, toolStrip2 As ToolStrip
    Private WithEvents toolStripTextBox1 As ToolStripTextBox
    Private WithEvents goButton, backButton, forwardButton, _
        stopButton, refreshButton, homeButton, _
        searchButton, printButton As ToolStripButton

    Private statusStrip1 As StatusStrip
    Private toolStripStatusLabel1 As ToolStripStatusLabel

    Private Sub InitializeForm()

        webBrowser1 = New WebBrowser()

        menuStrip1 = New MenuStrip()
        fileToolStripMenuItem = New ToolStripMenuItem()
        saveAsToolStripMenuItem = New ToolStripMenuItem()
        toolStripSeparator1 = New ToolStripSeparator()
        printToolStripMenuItem = New ToolStripMenuItem()
        printPreviewToolStripMenuItem = New ToolStripMenuItem()
        toolStripSeparator2 = New ToolStripSeparator()
        exitToolStripMenuItem = New ToolStripMenuItem()
        pageSetupToolStripMenuItem = New ToolStripMenuItem()
        propertiesToolStripMenuItem = New ToolStripMenuItem()

        toolStrip1 = New ToolStrip()
        goButton = New ToolStripButton()
        backButton = New ToolStripButton()
        forwardButton = New ToolStripButton()
        stopButton = New ToolStripButton()
        refreshButton = New ToolStripButton()
        homeButton = New ToolStripButton()
        searchButton = New ToolStripButton()
        printButton = New ToolStripButton()

        toolStrip2 = New ToolStrip()
        toolStripTextBox1 = New ToolStripTextBox()

        statusStrip1 = New StatusStrip()
        toolStripStatusLabel1 = New ToolStripStatusLabel()

        webBrowser1.Dock = DockStyle.Fill

        menuStrip1.Items.Add(fileToolStripMenuItem)

        fileToolStripMenuItem.DropDownItems.AddRange( _
            New ToolStripItem() { _
                saveAsToolStripMenuItem, toolStripSeparator1, _
                pageSetupToolStripMenuItem, printToolStripMenuItem, _
                printPreviewToolStripMenuItem, toolStripSeparator2, _
                propertiesToolStripMenuItem, exitToolStripMenuItem _
            })

        fileToolStripMenuItem.Text = "&File"
        saveAsToolStripMenuItem.Text = "Save &As..."
        pageSetupToolStripMenuItem.Text = "Page Set&up..."
        printToolStripMenuItem.Text = "&Print..."
        printPreviewToolStripMenuItem.Text = "Print Pre&view..."
        propertiesToolStripMenuItem.Text = "Properties"
        exitToolStripMenuItem.Text = "E&xit"

        printToolStripMenuItem.ShortcutKeys = Keys.Control Or Keys.P

        toolStrip1.Items.AddRange(New ToolStripItem() { _
            goButton, backButton, forwardButton, stopButton, _
            refreshButton, homeButton, searchButton, printButton})

        goButton.Text = "Go"
        backButton.Text = "Back"
        forwardButton.Text = "Forward"
        stopButton.Text = "Stop"
        refreshButton.Text = "Refresh"
        homeButton.Text = "Home"
        searchButton.Text = "Search"
        printButton.Text = "Print"

        backButton.Enabled = False
        forwardButton.Enabled = False

        toolStrip2.Items.Add(toolStripTextBox1)
        toolStripTextBox1.Size = New System.Drawing.Size(250, 25)

        statusStrip1.Items.Add(toolStripStatusLabel1)

        Controls.AddRange(New Control() { _
            webBrowser1, toolStrip2, toolStrip1, _
            menuStrip1, statusStrip1, menuStrip1})
    End Sub

    <STAThread()> _
    Public Shared Sub Main()
        Application.EnableVisualStyles()
        Application.Run(New Form1())
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Dim obj As WrapperClass
        'obj.Login("https://us.battle.net/login/en/login.xml?ref=https://www.worldofwarcraft.com/account/&app=wam&cr=true", "https://www.worldofwarcraft.com/account/index-trial.html", "accountName", "sriresume@homail.com", "Behard321", "password")
        'Dim str As String
        'str = obj.GetWebPageContent()

    End Sub
    Public Sub Test()

        Me.Hide()

        MyFirstForm.Show()

    End Sub
    Private _strUserName
    Private _strPassword

    Public Property strUserName()
        Get
            Return _strUserName
        End Get
        Set(ByVal value)
            _strUserName = value

        End Set
    End Property

    Public Property strPassoword()
        Get
            Return _strPassword
        End Get
        Set(ByVal value)
            _strPassword = value

        End Set
    End Property

End Class
0
 
LVL 19

Expert Comment

by:Shahan Ayyub
ID: 33916018
Hi!

My Apologizes for the delay response. I checked the code. I don't think so that the error is due to the control because yahoo and google mails are logging via the same Webbrowser control. I think there is something wrong with the page. I have tested by using minimal code you provided it is not working. I can't help you on that. You should ask admin for some additional experts.
0
 

Author Comment

by:futurebuilders
ID: 34478695
thanks
0
 

Author Closing Comment

by:futurebuilders
ID: 34510110
thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now