If you don't absolutely have to use the mshtml interface you could try this:
Imports System.IO
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Set the webbrowser control visible property to false if you don't need it for anything else.
WebBrowser1.Url = New Uri("C:\Test1.htm")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim htmlDocument As HtmlDocument = WebBrowser1.Document
ListBox1.Items.Clear()
' Iterate all the elements and display tag names
For Each element As HtmlElement In htmlDocument.All
ListBox1.Items.Add(element.TagName)
If element.TagName.ToUpper = "IMG" Then
ListBox2.Items.Add(element.DomElement.src)
End If
Next
End Sub
End Class
tinstaafl
Nearly a Posting Virtuoso
1,478 posts since Jun 2010
Reputation Points: 429
Solved Threads: 261
Skill Endorsements: 14
Try recasting the document to a IHTMLDocument3 use getElementsByTagName on the new cast object,
TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418
Solved Threads: 149
Skill Endorsements: 13
I have tested this by casting the webbrowser.Document.DomDocument, so hopefully it will work for you.
You used: Dim htmlDocument As mshtml.IHTMLDocument2 = DirectCast(New mshtml.HTMLDocument(), mshtml.IHTMLDocument2)
recast as mshtml.IHTMLDocument3
Dim doc3 As mshtml.IHTMLDocument3 = DirectCast(htmlDocument, IHTMLDocument3)
For Each img As mshtml.IHTMLImgElement In doc3.getElementsByTagName("img")
Debug.WriteLine(img.src)
Next
TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418
Solved Threads: 149
Skill Endorsements: 13
src is the file path for images, and my original code works for that. To get the inner text for Title use:
If element.TagName.ToUpper = "TITLE" Then
ListBox2.Items.Add(element.InnerText)
End If
tinstaafl
Nearly a Posting Virtuoso
1,478 posts since Jun 2010
Reputation Points: 429
Solved Threads: 261
Skill Endorsements: 14
Question Answered as of 3 Months Ago by
TnTinMN
and
tinstaafl