获取 WPF WebBrowser HTML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25557474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get WPF WebBrowser HTML
提问by czubehead
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).
我正在使用 Wpf WebBrowser 访问某个页面。我需要获取它的 HTML 内容 - 我不能使用 Webclient 或 WebReques 等,因为我需要在这些页面上执行 JS。我也试过 Awesomium 和 Wf WebBrowser(都错了)。
dynamic doc=browser.Document;
var text=doc.InnerHtml//or something like this
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
上面的代码对我不起作用,它显示空引用。谁能告诉我怎么取?我一直在寻找这个数周,但没有找到任何真正有效的东西:/。请回答像你能想象到的最大的笨蛋:D。有时我会遇到人们给我发送一段代码,我不知道如何使用它......我的意思是请让你的帖子像结尾一样
string HTML=some_stuff;
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.
或者,如果您知道一些没有问题的替代浏览器,以及我可以在何处访问 HTML 或可以让我在加载的 Html 上执行 JS 的内容,并且会影响 cookie 和 HTML 源代码中的更改,这也是一个非常好的答案。我将不胜感激任何帮助。
回答by czubehead
Yeeeaaaah! I did it. It's so simple:
啊啊啊啊!我做到了。就这么简单:
string HTML = (browser.Document as mshtml.IHTMLDocument2).body.outerHTML;
回答by Gray
I made something like this once. It was horrible, but it works.
我曾经做过这样的事情。这很可怕,但它有效。
You need to add a reference to Microsoft.mshtml.
您需要添加对Microsoft.mshtml.
Then you can use IHTMLDocument2. Why 2? Good question... anyway, I wrote a couple of helper functions like this:
然后你可以使用IHTMLDocument2. 为什么是2?好问题……无论如何,我写了几个这样的辅助函数:
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;
}
Executing JS
执行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);");
回答by user2250152
Did you try wpf WebBrowser method called InvokeScript()?
您是否尝试过名为 InvokeScript() 的 wpf WebBrowser 方法?
http://msdn.microsoft.com/en-us/library/cc491132(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/cc491132(v=vs.110).aspx
string HTML = webBrowser.InvokeScript(@"document.getElementsByTagName ('html')[0].innerHTML").ToString();
回答by Norman
When I tried @Gray or @czubehead's code bodywas always null. The following code, however, worked for me:
当我尝试@Gray 或@czubehead 时,代码body始终为空。但是,以下代码对我有用:
dynamic webBrowserDocument = webBrowser.Document;
string html = webBrowserDocument?.documentElement?.InnerHtml;
And make sure that this should go into LoadCompletedor later. When using this in Navigatedthe source is not complete or even null.
并确保这应该进入LoadCompleted或稍后。当在Navigated源中使用此不完整甚至null.

