获取WPF WebBrowser HTML [英] get WPF WebBrowser HTML

查看:313
本文介绍了获取WPF WebBrowser HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Wpf WebBrowser来访问某个页面。我需要得到它的HTML内容 - 我不能使用Webclient或WebReques等因为我需要在那些页面上执行JS。我也尝试过Awesomium和Wf WebBrowser(都错了)。

I'm using Wpf WebBrowser to access a certain page. I need to get it's HTML content- I can't use Webclient or WebReques etc. because I need to execute JS on that pages. I also tried Awesomium and Wf WebBrowser (both wrong).

    dynamic doc=browser.Document;
    var text=doc.InnerHtml//or something like this

上面的代码没有为我工作,它显示无效参考。
有人能告诉我怎么去取它吗?我已经搜索了好几个星期,但没有发现任何真正有用的东西:/。请回答一下你能想象到的最大笨蛋:D。有时候我会发现人们给我发了一段代码,我不知道如何使用它...我的意思是请让你的帖子像

Code above doesn't work for me, it shows nullreference. Can anybody tell me how to fetch it? I've been searching for this for weeks and didn't find anything really working :/ . Please answer like for a biggest dumbass you can imagine :D. It sometimes happens to me that people send me a piece of code and I have no idea how to use it... I mean please make your posts like ending with

     string HTML=some_stuff;

或者如果你知道一些没有错误的替代浏览器以及我可以访问HTML或其他东西的地方让我在加载的Html上执行JS,像cookies一样影响和HTML源代码的变化,这也是一个非常好的答案。
我会感激任何帮助。

Or if you know about some alternative browser which is not buggy and where I can access HTML or something that would let me execute JS on loaded Html with affects like cookies and changes in HTML source that's also a really good answer. I'll be appreciative for any help.

推荐答案

我曾经做过这样的事情。这太可怕了,但确实有效。

I made something like this once. It was horrible, but it works.

您需要添加对 Microsoft.mshtml

You need to add a reference to Microsoft.mshtml.

然后你可以使用 的IHTMLDocument2 。为什么2?好问题......无论如何,我写了几个这样的辅助函数:

Then you can use IHTMLDocument2. Why 2? Good question... anyway, I wrote a couple of helper functions like this:

public static void FillField(object doc, string id, string value)
{
    var element = findElementByID(doc, id);
    element.setAttribute("value", value);
}

public static void ClickButton(object doc, string id)
{
    var element = findElementByID(doc, id);
    element.click();
}

private static IHTMLElement findElementByID(object doc, string id)
{
    IHTMLDocument2 thisDoc;
    if (!(doc is IHTMLDocument2))
        return null;
    else
        thisDoc = (IHTMLDocument2)doc;

    var element = thisDoc.all.OfType<IHTMLElement>()
        .Where(n => n != null && n.id != null)
        .Where(e => e.id == id).First();
    return element;
}



执行JS



Executing JS

private static void ExecuteScript(object doc, string js)
{
    IHTMLDocument2 thisDoc;
    if (!(doc is IHTMLDocument2))
        return;
    else
        thisDoc = (IHTMLDocument2)doc;
    thisDoc.parentWindow.execScript(js);
}

我称之为......

I call them like this...

HtmlDocumentHelper.FillField(webBrowser.Document, <id>, <value>);
HtmlDocumentHelper.FillField(webBrowser.Document, <id>, <value>);
HtmlDocumentHelper.ClickButton(webBrowser.Document, <id>);
HtmlDocumentHelper.ExecuteScript(webBrowser.Document, "alert(1);");

这篇关于获取WPF WebBrowser HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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