Heim > Web-Frontend > js-Tutorial > Hauptteil

Wie rufe ich dynamisch generierten HTML-Code aus Webbrowser-Steuerelementen ab?

DDD
Freigeben: 2024-10-18 08:35:03
Original
272 Leute haben es durchsucht

How to Retrieve Dynamically Generated HTML Code from Web Browser Controls?

How to Dynamically Generate HTML Code Using .NET's WebBrowser or mshtml.HTMLDocument?

Problem:

Retrieving dynamically generated HTML code from a web page using the WebBrowser class or mshtml.HTMLDocument interface can be a challenge. The WebBrowser class fails to capture the rendered HTML, and mshtml.HTMLDocument returns raw HTML that differs from the actual page content.

Solution:

Using WebBrowser Class:

Although the WebBrowser class does not provide a direct method for obtaining the rendered HTML, it is possible to implement a workaround. Add a WebBrowser control to a form, have it navigate to the desired URL, and then use the following steps to retrieve the HTML:

  1. Send "CTRL + A" keys to select all content.
  2. Use the Copy method to copy the selection to the clipboard.
  3. Paste the HTML from the clipboard and parse it as needed.

Using mshtml.HTMLDocument Interface:

  1. Create an instance of mshtml.HTMLDocument and pass it the downloaded HTML using write.
  2. Check for changes in the HTML snapshot by polling the all property and the IsBusy property of the WebBrowser control.
  3. Once the IsBusy property becomes false and there are no changes in the HTML snapshot, consider the page to be fully rendered and retrieve the HTML.

Additional Considerations:

  • Ensure HTML5 rendering is enabled using Browser Feature Control.
  • Use a timeout to prevent infinite rendering.
  • Async/await can simplify the implementation of the polling logic.

Example Code:

<code class="C#">using Microsoft.Win32;
using System;
using System.Threading;
using System.Threading.Tasks;
using mshtml;

public async Task<string> LoadDynamicPage(string url, CancellationToken token)
{
    var doc = new HTMLDocument();
    doc.write(new System.Net.WebClient().DownloadString(url));

    // Poll for changes in HTML snapshot
    var html = doc.documentElement.outerHTML;
    while (true)
    {
        await Task.Delay(500, token);
        var htmlNow = doc.documentElement.outerHTML;
        if (html == htmlNow)
            break;

        html = htmlNow;
    }

    return html;
}</code>
Nach dem Login kopieren

Das obige ist der detaillierte Inhalt vonWie rufe ich dynamisch generierten HTML-Code aus Webbrowser-Steuerelementen ab?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Quelle:php
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!