如何使用 mshtml 获取框架的内容?

发布于 2024-07-09 16:03:06 字数 952 浏览 13 评论 0原文

问题是:

我在 IE 中有一个钩子,它对 WebBrowser.OnNavigateComplete2 事件做出反应,以解析文档内容以获得一些精确信息。

该文档包含框架,因此我查看了 HTMLDocument.frames。 对于每一个,我都会查看 document.body.outerHTML 属性来检查内容。

问题是,我正在寻找的字符串永远不会显示在那里,而它显示在结局页面中。 那么,我是不是找错地方了? 如果在页面完全加载时显示它,那么它会在某个时刻下载,对吧? 但我应该看哪个物体呢?

顺便说一句,我不知道这是否重要,但我正在搜索的页面来自 ASP.NET 应用程序。

public void OnNavigateComplete2(object pDisp, ref object url)
{
    document = (HTMLDocument)webBrowser.Document;

    mshtml.FramesCollection frames = document.frames;
    for (int i = 0; i < frames.length; i++)
    {
        object refIdx = i;
        IHTMLWindow2 frame = (IHTMLWindow2)frames.item(ref refIdx);
        string frameContent = frame.document.body.outerHTML;
    }
}

感谢您的帮助。


@公羊 每个页面都会多次启动此事件,因此我认为每次加载框架时都会发生此事件,即使我没有抓住我正在寻找的那个。 如果不是,捕获帧内容的事件是什么?

我想要做的是检测精确帧上的一些精确信息,然后保存它。 后来,由某些用户操作触发加载网页,我需要从解析框架中获得的信息。

Here's the issue:

I have a hook in IE that reacts on WebBrowser.OnNavigateComplete2 event to parse the content of the document for some precise info.

That document contains frames, so I look into the HTMLDocument.frames. For each one, I look into the document.body.outerHTML property to check for the content.

Problem is, the string I'm looking for never shows there, whereas it is displayed in the finale page. So, am I looking in the wrong place? If it is displayed when the page is fully loaded, then it's downloaded at some point, right? But in which object should I look ?

BTW, I Don't know if that is of any importance, but the page I'm searching into comes from a ASP.NET application.

public void OnNavigateComplete2(object pDisp, ref object url)
{
    document = (HTMLDocument)webBrowser.Document;

    mshtml.FramesCollection frames = document.frames;
    for (int i = 0; i < frames.length; i++)
    {
        object refIdx = i;
        IHTMLWindow2 frame = (IHTMLWindow2)frames.item(ref refIdx);
        string frameContent = frame.document.body.outerHTML;
    }
}

Thank your for your help.


@rams
This event is launched many times for each page, so I figured it was each time a framed is loaded, even if i don't get to catch the one I'm looking for. If not, what would be the event to catch the frames content?

What I want to do is detect some precise info on a precise frame, then save it. later, a web page is loaded triggered by some user action, where I need the info I got from parsing the frame.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清风挽心 2024-07-16 16:03:06

您知道您正在寻找内容的框架的名称/ID 吗? 之类的框架的引用

iFrame frm = document.frames(<your frame id>);

int readyState=0;

while(frm.readystate !=4){
// do nothing. be careful to not create an endless loop
}

if(frm.readyState==4){
   // get your content now
}

如果是这样,在您的navigateComplete2事件中,您能否获得对HTH

Do you know the name/id of the frame you are looking for content? If so, in your navigateComplete2 event, can you get a reference to the frame like

iFrame frm = document.frames(<your frame id>);

int readyState=0;

while(frm.readystate !=4){
// do nothing. be careful to not create an endless loop
}

if(frm.readyState==4){
   // get your content now
}

HTH

青衫负雪 2024-07-16 16:03:06

您是否使用某种线程? 在单独的线程中运行浏览器确实会让事情变得混乱。 尝试在 STAThread 中执行它并检查是否得到正确的结果。

Are you using some kind of threading? Running the browser in a separate thread really messes up things. Try to execute it in an STAThread and check if you get the correct result.

段念尘 2024-07-16 16:03:06

您的字符串不显示的原因是框架的原因。 Web 浏览器控件在加载主文档后会触发文档导航完成事件。 目前,这些框架尚未询问其来源。 Web 浏览器控件解析文档后,将发出并下载对框架源的请求。

您能描述一下您想要实现的目标吗?

The reason your string does not show is because of the frame. The web browser control fires the document navigate complete event after it has loaded the main document. At this point, the frames haven't yet requested their sources. After the document is parsed by the web browser control, requests for the frame sources are issues and downloaded.

Can you please describe what you are trying to accomplish?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文