How to press a login button of a website programmatically

前端 未结 1 1878
没有蜡笔的小新
没有蜡笔的小新 2021-01-22 01:20

I am loading a website into a WebBrowser in a winform. I wish to login using a log and pass automatically via vb.net

I have the following code executing onc

相关标签:
1条回答
  • 2021-01-22 02:04

    I applied this logic:

    • Put the code in a DocumentCompleted event to run the code after the document completed.
    • The DocumentCompleted also raises when an iframe document completed, so to get rid of iframes use a criteria like comparing e.Url with the base url know.
    • Find the submit button by name and click on it.

    And it worked properly. It redirected to the same url and showed a red not authenticated message.

    Code:

    Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        If (e.Url.ToString().StartsWith("https://www.weatherzone.com.au")) Then
            WebBrowser1.Document.GetElementById("e").SetAttribute("value", "email@address.com")
            WebBrowser1.Document.GetElementById("p").SetAttribute("value", "password")
            WebBrowser1.Document.All().GetElementsByName("target").Cast(Of HtmlElement).First().InvokeMember("click")
        End If
    End Sub
    
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WebBrowser1.Navigate("https://www.weatherzone.com.au/customise.jsp")
    End Sub
    

    To make the code more stable, add required null checkings.

    0 讨论(0)
提交回复
热议问题