Hi,


I am trying to get the html source of the particular webpage that having different frames.i want the html source of a particular frame.

Thank you in advance

Dani AI

Generated

— both answers from and point in the right direction: the embedded WebBrowser exposes the page DOM, so you can target a single frame. Two reliable patterns are: (A) read the frame document directly from the WebBrowser DOM when the page and frame are same-origin, or (B) locate the frame element and download its src URL separately. Pick (A) when the frame needs the same session/cookies or has inline content, pick (B) when the frame is just a URL that you can fetch.

Example (pattern B — find the frame element and download its src). This works well for <iframe> or <frame> elements that use a separate URL:

Dim frames = WebBrowser1.Document.GetElementsByTagName("iframe")
For Each fe As HtmlElement In frames
    If fe.GetAttribute("id") = "targetId" OrElse fe.GetAttribute("name") = "targetName" Then
        Dim src As String = fe.GetAttribute("src")
        If Not String.IsNullOrEmpty(src) Then
            Dim html As String = (New System.Net.WebClient()).DownloadString(src)
            ' html now contains the frame source
        End If
    End If
Next

If you need the live DOM (inline frame, authenticated session, or script-generated content), use the WebBrowser DOM instead of a separate HTTP fetch. A common approach is to inject a tiny JavaScript helper (via InvokeScript) that returns the inner HTML of the target frame; this avoids cross-thread COM casting and works from the UI thread.

Practical tips:

  • Wait for the page to finish loading. DocumentCompleted fires multiple times for frames; compare e.Url to the top-level WebBrowser.Url to detect final completion.
  • Same-origin policy prevents reading a cross-domain frame DOM; in that case use pattern B or copy cookies into your HTTP client.
  • If you need COM DOM types, add the Microsoft.mshtml reference and work with the frames collection carefully.

See the WebBrowser events and WebClient docs for details: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.webbrowser.documentcompleted and https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient.downloadstring. For why cross-domain DOM access can fail, see the same-origin policy notes: https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy.

Recommended Answers

All 2 Replies

I didn't test this at all but you could try to use WebBrowser1.Document.GetElementById method. Few other methods or properties that might help you are WebBrowser1.Document.ActiveElement and WebBrowser1.Document.DomDocument . See the web browser control's documentation how to use them and what they do. There's no direct method to obtain either frame or single frame's HTML. Unless you use WebBrowser1.DocumentText and write your own HTML parser :)

HTH

Hi,


I am trying to get the html source of the particular webpage that having different frames.i want the html source of a particular frame.

Thank you in advance

Working with the WebBrowser control you can access the Frames within any document.

WebBrowser1.document.window.frames("FrameName").document.body.innerHTML

If yo don't have the name of the frame, simply traverse through the each of the HTMLElement objects checking for the TagName="FRAME" to obtain it's name.

Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.