如何在Frames/IFrames中获取HtmlElement值? [英] How to get an HtmlElement value inside Frames/IFrames?

查看:223
本文介绍了如何在Frames/IFrames中获取HtmlElement值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Winforms WebBrowser控件从下面链接的站点收集视频剪辑的链接.

I'm using the Winforms WebBrowser control to collect the links of video clips from the site linked below.

但是,当我逐个滚动元素时,找不到<video>标签.

But, when I scroll element by element, I cannot find the <video> tag.

void webBrowser_DocumentCompleted_2(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    try
    {
        HtmlElementCollection pTags = browser.Document.GetElementsByTagName("video");
        int i = 1;
        foreach (HtmlElement link in links)
        {

            if (link.Children[0].GetAttribute("className") == "vjs-poster")
            {
                try
                {

                    i++;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }   // Added by edit
}

使用后很快

HtmlElementCollection pTags = browser.Document.GetElementsByTagName("video");

我已经返回0

我需要调用任何ajax吗?

Do I need to call any ajax?

推荐答案

您链接的网页包含 HtmlDocument .到目前为止,您只解析主Document容器.
因此,您需要解析其他一些FrameHtmlElements标记.
WebBrowser引用了网页框架"列表. .Document.Window.Frames 属性,该属性返回 HtmlWindow 都包含它自己的HtmlDocument对象.

The Web page you linked contains IFrames.
An IFrame contains its own HtmlDocument. As of now, you're parsing just the main Document container.
Thus, you need to parse the HtmlElements TAGs of some other Frame.
The Web Page Frames list is referenced by the WebBrowser.Document.Window.Frames property, which returns an HtmlWindowCollection.
Each HtmlWindow in the collection contains it own HtmlDocument object.

大多数情况下,我们需要解析Frames集合中的每个HtmlWindow.Document,而不是解析WebBrowser返回的Document对象属性.除非,当然,除非我们已经知道必需的元素是主文档或另一个已知的Frame.

Instead of parsing the Document object property returned by a WebBrowser, we, most of the time, need to parse each HtmlWindow.Document in the Frames collection; unless, of course we already know that the required Elements are part of the main Document or another known Frame.

一个示例(与当前任务有关):

An example (related to the current task):

  • 订阅
  • Subscribe the DocumentCompleted event of the WebBrowser Control/Class.
  • Check the WebBrowser.ReadyState property to verify that a Document is loaded completly.

注意:
记住网页可能由Frames/IFrames中包含的多个Document组成,如果使用ReadyState = WebBrowserReadyState.Complete多次引发该事件,我们不会感到惊讶. 当WebBrowser加载完毕后,每个Frame的Document都会引发该事件.

Note:
Remembering that a Web Page may be composed by multiple Documents contained in Frames/IFrames, we won't be surprised if the event is raised multiple times with a ReadyState = WebBrowserReadyState.Complete.
Each Frame's Document will raise the event when the WebBrowser is done loading it.

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆