5

Vb.Net WinForms アプリケーションで SHDocVw.InternetExplorer API を使用して、Internet Explorer から要素を取得しています。親ドキュメントとフレーム要素内の要素には簡単にアクセスできますが、「埋め込み」コンテナ内の要素にはアクセスできません。サンプルコードは次のとおりです。

    Dim ie As SHDocVw.InternetExplorer
    ie.Navigate("Some URL")
    ie.Visible = True
    Dim ieDoc As mshtml.IHTMLDocument2 = ie.Document

    'All Elements
    Dim allElements = ieDoc.all

    'Frames
    Dim allFrames = ieDoc.frames

    'Fetch each frame and use its document to get all elements

    Dim allEmbed = ieDoc.embeds

    'How to fetch document inside embed to access its elements?

サンプルの html は次のとおりです。

サンプル.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Sample</title> 
</head>
<body>
	<embed src="test.html" name="test1"/>	
</body>
</html>
   

Test.html

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Sample</title> 
</head>
<body bgcolor="#FFFFFF">
	<button>Button1</button>
	<label>Test 1</label>
</body>	
</html>

「embed」タグを使用して、Sample.html にロードされた Test.html 内のボタンとラベルにアクセスするにはどうすればよいですか?

編集1

私の調査によると、「オブジェクト」要素の .contentDocument プロパティを使用して「オブジェクト」コンテナ内のドキュメントにアクセスできますが、「埋め込み」コンテナでは同じことが機能しません。

「埋め込み」コンテナの getSVGDocument() プロパティを使用して comObject を取得できますが、それを mshtml.IHTMLDocument2 にキャストすることはできません

4

1 に答える 1

0

さて、私はここでhtmlを解析するために「Html Agility Pack」を使用してきましたが、それは非常に素晴らしいです.ページ内のすべての埋め込み要素を取得し、内部コンテンツを読み取り/解析することができます. http://html-agility-pack.net/

私のサンプル:

'<html xmlns='http://www.w3.org/1999/xhtml'>
'<head>
'    <title>Sample</title> 
'</head>
'<body>
'    <embed src='http://stackoverflow.com/questions/41806246/access-elements-inside-html-embed-tag-source-html-using-vb-net' name='test1'/>
'</body>
'</html>
'The htmlCode string:

Dim htmlCode As String = "<html xmlns='http://www.w3.org/1999/xhtml'><head><title>Sample</title></head><body><embed src='http://stackoverflow.com/questions/41806246/access-elements-inside-html-embed-tag-source-html-using-vb-net' name='test1'/></body></html>";

Dim client As New WebClient()

Dim doc = New HtmlDocument()
doc.LoadHtml(htmlCode)

Dim nodes = doc.DocumentNode.Descendants("embed")

For Each item As var In nodes
    Dim srcEmded = item.GetAttributeValue("src", "")

    If Not String.IsNullOrWhiteSpace(srcEmded) Then

        Dim yourEmbedHtml As String = client.DownloadString(srcEmded)
        'Do what you want with yourEmbedHtml

    End If
Next
于 2017-01-31T15:58:19.897 に答える