Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
823 views
in Technique[技术] by (71.8m points)

vb.net - Auto Website Login: SetAttribute not working

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     
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You don't need any async procedure here. The WebBrowser.DocumentCompleted event is already invoked asynchronously. DoEvents() is equally useless if not disruptive.

You just need to subscribe to the DocumentCompleted event and call the Navigate method to let the WebBrowser load the remote Html resource.

When the HtmlDocument is finally loaded, the WebBrowser will signal its completion setting its state to WebBrowserReadyState.Complete.

About the Html Input Element and Forms:
here, the code is supposing that there's only one Form in that HtmlDocument.
It may be so, but it may be not. A Html Document can have more than one Form and each Frame can have its own Document. IFrames will have one each for sure.

Read the notes in this answer (C# code, but you just need the notes) for more information on how to handle multiple Frames/IFrames


Button1 will wire up the DocumentCompleted event and call Navigate().
When the Document is completed, the code in the event handler will run and perform the LogIn procedure.
The event handler is then removed, since it has complelted its task and you still need to use the WebBrowser for other purposes.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Button1.Enabled = False
    WebSiteLogIn()
End Sub

Private Sub WebSiteLogIn()
    AddHandler WebBrowser1.DocumentCompleted, AddressOf PageWaiter
    WebBrowser1.Navigate("https://thesite.com/#/login")    
End Sub

Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
    If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
        WebBrowser1.Document.GetElementById("username").SetAttribute("value", "Username")
        WebBrowser1.Document.GetElementById("Password").SetAttribute("value", "Password")

        Dim allInputElements = WebBrowser1.Document.Body.All.
            Cast(Of HtmlElement).Where(Function(h) h.TagName.Equals("INPUT")).ToList()

        For Each element As HtmlElement In allInputElements
            If element.GetAttribute("type").ToUpper().Equals("SUBMIT") Then
                element.InvokeMember("click")
            End If
        Next

        RemoveHandler WebBrowser1.DocumentCompleted, AddressOf PageWaiter
        Button1.Enabled = True
    End If
End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...