5

I’m using SHDocVw.InternetExplorer APIs in my Vb.Net WinForms application to fetch elements from Internet Explorer. I can easily access the elements inside parent document and frame elements but I am not able to access the elements inside the 'embed' container. Here's the sample code:

    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?

And here's a sample html:

Sample.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>

How can I access the button and label inside the Test.html loaded in Sample.html using 'embed' tag?

Edit 1:

As per my research I can access the document inside the 'object' container using the .contentDocument property of 'object' element but the same is not working for 'embed' container.

I can get some comObject using getSVGDocument() property on 'embed' container but not able to cast it to mshtml.IHTMLDocument2

prem
  • 3,348
  • 1
  • 25
  • 57
  • Do you have a full reproducing project? embed does not always work depending on security settings, IE versions, or whatever context. – Simon Mourier Jan 28 '17 at 08:19
  • Yes, I have the reproducing project. Also the code shared in this post is enough to reproduce the issue. Embed is working fine in my IE version 11. The issue I am facing is in fetching the HTMLDocument insied the Embed container. I am updating the question with my new findings. – prem Jan 30 '17 at 06:44
  • I asked for a reproducing project because you code is not enough to reproduce. Please post a full sample if you need help. – Simon Mourier Jan 30 '17 at 07:05
  • Is there a way to share complete sample project created using Visual Studio on SO? – prem Jan 30 '17 at 07:18
  • 1
    Here's the link with complete sample project https://www.dropbox.com/s/64xsu0kl4eiwhyt/EmbedSample.zip?dl=0 – prem Jan 30 '17 at 09:19
  • this may be worth reading [Plugins that cannot be secured are disabled in sandboxed browsing contexts](http://www.w3.org/TR/html5/embedded-content-0.html#the-embed-element), plus: from the specification of the `embed` element, it is not obvious that it could be placed outside a `body` as you do in your Sample.html. Did you try with ` ... ` around it? – Cee McSharpface Jan 30 '17 at 12:19
  • I don't think you'll like this idea, but, if it's your html your hosting in the embed, then you can run a script from within to pass back the document and window objects. eg, parent.embedDoc = document. – user4749485 Jan 31 '17 at 03:05
  • No, it's not my html. Client is using it on their application. – prem Jan 31 '17 at 06:06
  • @dlatikay Tried with ... also, Still no difference. This issue has nothing to do with this html. It's just for reference, The issue can be seen on any online embed sample also such as this link http://xn--dahlstrm-t4a.net/svg/html/get-embedded-svg-document-script.html – prem Jan 31 '17 at 06:21
  • I'm afraid there is no standard to get `embed` inner content as DOM, beyond what you get with IHTMLEmbedElement. Plus it's deprecated since HTML 4: https://msdn.microsoft.com/library/ms535245.aspx , replacement being `object` which works fine as you already found out. – Simon Mourier Jan 31 '17 at 22:56
  • @Simon Mourier: Sir, I can get some Com object using getSVGDocument() method in my sample project. Is this the HTMLDocument object which I require? If yes, then please guide me How can I cast this com object to .Net HTMLDocument? – prem Feb 01 '17 at 07:31
  • Like I said, I don't think you can. getSVGDocument is very specific for embedded SVG documents. – Simon Mourier Feb 01 '17 at 09:05

1 Answers1

0

Well, I have been using "Html Agility Pack" to parse html over here and it's pretty awesome, you can get all embed elements in your page and them read/parse the inner content. http://html-agility-pack.net/

My sample:

'<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
wp78de
  • 18,207
  • 7
  • 43
  • 71
  • Can't use agility pack as my motive not just to parse the content, I am implementing various events on browser elements to capture real time user actions. – prem Feb 01 '17 at 06:45