Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以HTML格式获取当前WebBrowser DOM_C#_Html_Video_Xpath - Fatal编程技术网

C# 以HTML格式获取当前WebBrowser DOM

C# 以HTML格式获取当前WebBrowser DOM,c#,html,video,xpath,C#,Html,Video,Xpath,我想在WebBrowser上使用HTML功能包,它已经加载了我需要的所有东西(它单击一个带有代码的按钮来加载频道上的每个视频) (它加载YouTube频道,然后加载该频道上的所有视频。) 现在,如果我尝试获取所有视频的详细信息(我有一个工作代码,可以将频道的前30个视频放入列表视图),它仍将仅显示前30个视频,但我已将所有视频加载到WebBrowser页面(它显示所有视频) 我用它来获取当前从WebBrowser加载的内容 但它仍然只加载前30个视频,而不是从WebBrowser加载的所有视频

我想在WebBrowser上使用HTML功能包,它已经加载了我需要的所有东西(它单击一个带有代码的按钮来加载频道上的每个视频) (它加载YouTube频道,然后加载该频道上的所有视频。) 现在,如果我尝试获取所有视频的详细信息(我有一个工作代码,可以将频道的前30个视频放入列表视图),它仍将仅显示前30个视频,但我已将所有视频加载到WebBrowser页面(它显示所有视频) 我用它来获取当前从WebBrowser加载的内容


但它仍然只加载前30个视频,而不是从WebBrowser加载的所有视频。

如果目标网站大量使用AJAX(如Youtube),则很难确定页面何时完成加载和执行所有动态脚本。但您可以通过处理
window.onload
事件并为非确定性AJAX调用留出一到两秒钟的时间来实现。然后通过
dynamic
调用
webBrowser.Document.DomDocument.documentElement.outerHTML以获取当前呈现的HTML

例如:

private void Form1_Load(object sender, EventArgs e)
{
    DownloadAsync("http://www.example.com").ContinueWith(
        (task) => MessageBox.Show(task.Result),
        TaskScheduler.FromCurrentSynchronizationContext());
}

async Task<string> DownloadAsync(string url)
{
    TaskCompletionSource<bool> onloadTcs = new TaskCompletionSource<bool>();
    WebBrowserDocumentCompletedEventHandler handler = null;

    handler = delegate
    {
        this.webBrowser.DocumentCompleted -= handler;

        // attach to subscribe to DOM onload event
        this.webBrowser.Document.Window.AttachEventHandler("onload", delegate
        {
            // each navigation has its own TaskCompletionSource
            if (onloadTcs.Task.IsCompleted)
                return; // this should not be happening
            // signal the completion of the page loading
            onloadTcs.SetResult(true);
        });
    };

    // register DocumentCompleted handler
    this.webBrowser.DocumentCompleted += handler;

    // Navigate to url
    this.webBrowser.Navigate(url);

    // continue upon onload
    await onloadTcs.Task;

    // artificial delay for AJAX
    await Task.Delay(1000);

    // the document has been fully loaded, can access DOM here
    return ((dynamic)this.webBrowser.Document.DomDocument).documentElement.outerHTML;
}

您是否与当前使用的技术有关联?PhantomJS在这方面非常灵活。它将在页面最初加载时返回Html,而不是使用Ajax动态添加的任何内容。我如何获得动态加载的内容?wut。我有一个按钮,在加载页面并通过单击进行AJAX调用后,它将使用该页面。这个答案让我很困惑。哦,我误读了关于你如何加载页面的部分,对不起。只需使用最后一部分来获取当前页面的HTML快照:
((动态)this.webBrowser.Document.DomDocument).documentElement.outerHTML
而不是
webBrowser.DocumentText
。在清除非法字符的代码后,我得到这个
outerHTML
返回一个字符串,将其与
htmlabilitypack
一起使用,如下所示:
doc.LoadHtml(((动态)this.webBrowser.Document.DomDocument).documentElement.outerHTML)
。我使用了您的第一个代码,然后使用htmlabilitypack加载HTML!HtmlAgilityPack.HtmlDocument doc=新的HtmlAgilityPack.HtmlDocument();doc.LoadHtml(((动态)this.webBrowser1.Document.DomDocument.documentElement.outerHTML);
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(((dynamic)this.webBrowser1.Document.DomDocument).documentElement.ou‌​terHTML);