I'm creating a program dashboard and one feature is that the user is automatically logged into a website site using stored credentials in the program (no need to open chrome or FF).
In the program, await task delay is working, I see the username and password fields populate before submission (click), but when it tries to submit, the browser, built into the form, acts like the page is empty and no credentials have been entered? I should mention that I can see the username and password entered in the form, but the page acts like nothing has been entered. What am I doing wrong here?
Side note: The button on the site we're connecting to doesn't have an element ID, only a type is shown...hence the workaround for Invokemember("Click")
Any help is appreciated.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label3.Visible = False
End Sub
Private Function login_thesite() As Task
WebBrowser1.Document.GetElementById("username").SetAttribute("value", "Username")
WebBrowser1.Document.GetElementById("Password").SetAttribute("value", "Password")
Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
For Each webpageelement As HtmlElement In allelements
If webpageelement.GetAttribute("type") = "submit" Then
webpageelement.InvokeMember("click")
End If
Next
End Function
Private Property pageready As Boolean = False
#Region "Page Loading Functions"
Private Sub WaitForPageLoad()
AddHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
While Not pageready
Application.DoEvents()
End While
pageready = False
End Sub
Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
pageready = True
RemoveHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
End If
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If CheckBox1.Checked = True Then
login_thesite()
WaitForPageLoad()
End If
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
TextBox1.Text = ""
TextBox2.Text = ""
Label3.Visible = True
WebBrowser1.Navigate("https://thesite.com/#/login")
WaitForPageLoad()
End Sub
End Class