【问题标题】:get WPF WebBrowser HTML获取 WPF WebBrowser HTML
【发布时间】:2014-10-22 19:52:05
【问题描述】:

我正在使用 Wpf WebBrowser 访问某个页面。我需要获取它的 HTML 内容——我不能使用 Webclient 或 WebReques 等,因为我需要在这些页面上执行 JS。我还尝试了 Awesomium 和 Wf WebBrowser(都错了)。

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

上面的代码对我不起作用,它显示空引用。 谁能告诉我如何获取它?我一直在寻找这个好几个星期,但没有发现任何真正有用的东西:/。请回答你能想象到的最大笨蛋:D。有时我会遇到有人给我发一段代码,但我不知道如何使用它......我的意思是请让你的帖子像结尾一样

     string HTML=some_stuff;

或者,如果您知道一些没有错误的替代浏览器,并且我可以在哪里访问 HTML 或可以让我在加载的 Html 上执行 JS 并影响 cookie 和 HTML 源代码更改的东西,这也是一个非常好的答案。 我会很感激任何帮助。

【问题讨论】:

    标签: c# javascript wpf browser


    【解决方案1】:

    当我尝试@Gray 或@czubehead 的代码时,body 始终为空。但是,以下代码对我有用:

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

    并确保这应该进入LoadCompleted 或更高版本。在Navigated 中使用它时,源不完整,甚至null

    【讨论】:

      【解决方案2】:

      您是否尝试过名为 InvokeScript() 的 wpf WebBrowser 方法?

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

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

      【讨论】:

        【解决方案3】:

        耶耶耶!我做到了。就这么简单:

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

        【讨论】:

          【解决方案4】:

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

          您需要添加对Microsoft.mshtml 的引用。

          然后你可以使用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;
          }
          

          执行JS

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

          我这样称呼他们...

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

          【讨论】:

          • 感谢 mshtml 和 IHTMLDocument2!
          • 哈,很高兴它有帮助!祝你好运。
          猜你喜欢
          • 1970-01-01
          • 2011-04-24
          • 2015-07-10
          • 2014-07-26
          • 2018-10-15
          • 2014-05-25
          • 2012-05-07
          • 1970-01-01
          • 2013-09-19
          相关资源
          最近更新 更多