Answered Help! With GetElementsByTagName("A")

vmars316

Active member
Joined
Aug 24, 2020
Messages
39
Programming Experience
Beginner
Hello & Thanks ;
The following code doesn't work , no error msg , just returns nothing .
Pls , what am I doing wrong ?
Below this Sub is the full Code :

VB.NET:
    Private Sub Target_self_Click(sender As Object, e As EventArgs) Handles Target_self.Click
        Dim linkElements As HtmlElementCollection =
WebBrowser1.Document.GetElementsByTagName("A")
        '        Dim firstLink = linkElements.Cast(Of HtmlElement).FirstOrDefault()
        If linkElements.Count > 0 Then
            Dim firstLink = linkElements.Cast(Of HtmlElement).FirstOrDefault()
            If firstLink.GetAttribute("target") = "_blank" Then
                firstLink.SetAttribute("target", "_self")
            End If
        End If
        MsgBox("Target_self_Click =  " & "Target_self_Click")
    End Sub

Here is the full pgm code :


VB.NET:
Public Class GetElementsByTagName
    Private Sub GetElementsByTagName_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        WebBrowser1.ScriptErrorsSuppressed = True
        WebBrowser1.Navigate("http://vmars.us/SafeBrowser/SafeBrowserHome.html")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        WebBrowser1.Navigate(txbAddress.Text)
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        Dim PageElements As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("img")

        txbAddress.Text = WebBrowser1.Url.ToString
        For Each CurElement As HtmlElement In PageElements
            TextBox2.Text = TextBox2.Text & CurElement.GetAttribute("src") & Environment.NewLine
        Next
    End Sub

    Private Sub Target_self_Click(sender As Object, e As EventArgs) Handles Target_self.Click
        Dim linkElements As HtmlElementCollection =
WebBrowser1.Document.GetElementsByTagName("A")
        '        Dim firstLink = linkElements.Cast(Of HtmlElement).FirstOrDefault()
        If linkElements.Count > 0 Then
            Dim firstLink = linkElements.Cast(Of HtmlElement).FirstOrDefault()
            If firstLink.GetAttribute("target") = "_blank" Then
                firstLink.SetAttribute("target", "_self")
            End If
        End If
        MsgBox("Target_self_Click =  " & "Target_self_Click")
    End Sub

    Private Sub BackButton_Click(sender As Object, e As EventArgs) Handles BackButton.Click
        '        WebBrowser1.ScriptErrorsSuppressed = True
        WebBrowser1.Navigate("http://vmars.us/SafeBrowser/SafeBrowserHome.html")
    End Sub
End Class

GetElementsByTagName.png


Thanks for your Help...
 
I recommend that you start with tutorials to learn the basic programming elements of the language first. Let this rest until variables and loops is not a foreign concept anymore.
 
Hi JohnH ;
Thought I would report back on my progress , here is my current code :
VB.NET:
Public Class GetElementsByTagName
    Private Sub GetElementsByTagName_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        WebBrowser1.ScriptErrorsSuppressed = True
        WebBrowser1.IsWebBrowserContextMenuEnabled = False
        Me.MinimumSize = New Size(1000, 650)
        Me.StartPosition = FormStartPosition.CenterScreen
        Me.Left = 40
        Me.Top = 20
        WebBrowser1.Navigate("http://vmars.us/SafeBrowser/SafeBrowserHome.html")
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        Dim PageElements As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("img")
        ChangeBlankToSelf()
        txbAddress.Text = WebBrowser1.Url.ToString
    End Sub

    Private Sub ChangeBlankToSelf()
        Dim TimesThruCount = 0
        Dim linkElements As HtmlElementCollection =
               WebBrowser1.Document.GetElementsByTagName("A")
        '                Dim firstLink = linkElements.Cast(Of HtmlElement).FirstOrDefault()
        For Each link As HtmlElement In linkElements
            If link.GetAttribute("target") = "_blank" Then
                '                firstLink.SetAttribute("target", "_self")
                link.SetAttribute("target", "_self")
                EachSetLine.Text = EachSetLine.Text & ControlChars.NewLine & link.OuterHtml
            End If
            TimesThruCount = TimesThruCount + 1
        Next
    End Sub

    Private Sub Form1_ResizeEnd(sender As Object, e As EventArgs) Handles MyBase.ResizeEnd
        MsgBox("Form1 size: " & Me.Size.ToString)
    End Sub


    Private Sub BackBtn_Click(sender As Object, e As EventArgs) Handles BackBtn.Click
        WebBrowser1.GoBack()
    End Sub

    Private Sub ForwardBtn_Click(sender As Object, e As EventArgs) Handles ForwardBtn.Click
        WebBrowser1.GoForward()
    End Sub

    Private Sub RefreshBtn_Click(sender As Object, e As EventArgs) Handles RefreshBtn.Click
        WebBrowser1.Refresh()
    End Sub

    Private Sub GotoBtn_Click(sender As Object, e As EventArgs) Handles GotoBtn.Click
        WebBrowser1.Navigate(txbAddress.Text)
    End Sub

    Private Sub HomeBtn_Click(sender As Object, e As EventArgs) Handles HomeBtn.Click
        WebBrowser1.Navigate("http://vmars.us/SafeBrowser/SafeBrowserHome.html")
    End Sub
End Class

I am at a point now Where I need to Modify the registry so that program can't 'open new link in a new Tab' .
So You'll probably be hearing from me :)
Thanks for your Help...
 
Back
Top