Get WPF WebBrowser HTML

I use Wpf WebBrowser to access a specific page. I need to get HTML content - I cannot use Webclient or WebReques, etc., because I need to execute JS on these pages. I also tried Awesomium and Wf WebBrowser (both wrong).

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

The code above does not work for me, it shows the lack of a link. Can someone tell me how to get it? I searched for this for several weeks and did not find anything really working: /. Please answer as the biggest dumbass you can imagine: D. Sometimes it occurs to me that people send me a code and I have no idea how to use it ... I mean, please, your messages ended in

  string HTML=some_stuff; 

Or, if you know about some alternative browser that is not an error, and where I can access HTML or something that would allow me to run JS on the downloaded Html with the same effects as cookies and changes to HTML source, which is also a very good answer. I will be grateful for any help.

+5
source share
4 answers

I did something similar once. It was terrible, but it works.

You need to add a link to Microsoft.mshtml .

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 Execution

 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 that ...

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

Yeeeaaaah! I have done it. It is so simple:

  string HTML = (browser.Document as mshtml.IHTMLDocument2).body.outerHTML; 
+8
source

Have you tried using the wpf WebBrowser method called InvokeScript ()?

http://msdn.microsoft.com/en-us/library/cc491132(v=vs.110).aspx

 string HTML = webBrowser.InvokeScript(@"document.getElementsByTagName ('html')[0].innerHTML").ToString(); 
0
source

When I tried @Gray or @czubehead, the body code was always null. The following code, however, worked for me:

 dynamic webBrowserDocument = webBrowser.Document; string html = webBrowserDocument?.documentElement?.InnerHtml; 

And make sure it should go in LoadCompleted or newer. When using this parameter in Navigated source is not complete or even null .

0
source

Source: https://habr.com/ru/post/1201360/


All Articles