Click on input field of web browser ~ Get html element

simpleonline

Active member
Joined
Sep 13, 2011
Messages
33
Programming Experience
Beginner
So what I'm trying to do is setup my browser so that when I click an input field like a username, password, etc I would have a screen pop up where I can assign the "id" or "name" of the tag to a variable.

Here is a screenshot of what I am trying to do.

screenshot.jpg

I am able to create those pop up boxes using a custom Content Menu Strip.

Here is the code I have behind the context tab

VB.NET:
Private Sub UsernameToolStripMenuItem_Click(ByVal sender As  System.Object, ByVal e As System.EventArgs) Handles  UsernameToolStripMenuItem.Click
        wbMain.Document.GetElementById("username")


    End Sub

So from the example above what I am trying to accomplish is I want to be able to click on the username field (then the username menu pops up) and I want to assign the "id" or "name" of the element in the html to the variable of "username".

Got any ideas?

Thanks
 

azuckerman

Member
Joined
Sep 10, 2011
Messages
10
Location
Abilene, Texas, USA
Programming Experience
10+
Are you trying to do this in a browser window or from a Windows based application that is checking for some event in a browser?
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,583
Location
Norway
Programming Experience
10+
Handle the document Click event, look up the element. If it is a INPUT element show the context menu. For context menu item click set the elements value attribute.
Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    Me.doc = WebBrowser1.Document
End Sub

Private WithEvents doc As HtmlDocument
Private element As HtmlElement

Private Sub doc_Click(sender As Object, e As System.Windows.Forms.HtmlElementEventArgs) Handles doc.Click
    element = doc.GetElementFromPoint(e.MousePosition)
    If element.TagName = "INPUT" Then
        Me.ContextMenuStrip1.Show(Me.WebBrowser1.PointToScreen(e.MousePosition))
        Me.InputToolStripMenuItem.ShowDropDown()
    End If
End Sub

Private Sub NameToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles NameToolStripMenuItem.Click
    element.SetAttribute("value", "name")
End Sub

Private Sub PasswordToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles PasswordToolStripMenuItem.Click
    element.SetAttribute("value", "password")
End Sub
 
Top Bottom