【问题标题】:C# using WebBrowser control and need to access DOM elementsC# 使用 WebBrowser 控件并需要访问 DOM 元素
【发布时间】:2015-12-27 04:51:18
【问题描述】:

我在我的 WinForm 应用程序中使用 C# 加载了一个网页

我需要以编程方式将数据键入该页面上的特定字段(不使用 WATIN)。

如果有人有任何其他解决方案,我愿意接受。

相关页面没有 AJAX 或 JavaScript。它们是简单的 HTML 数据输入表单。

【问题讨论】:

    标签: c# winforms internet-explorer dom


    【解决方案1】:

    您可以使用WebBrowser 控件的Document 属性来执行此操作:

    C#代码:

    if (webBrowser1.Document == null) return;
    var form = webBrowser1.Document.Forms[0]; //form element
    var input = form.Children[0]; //input element
    input.SetAttribute("value","input value"); //set the input value
    form.InvokeMember("submit"); //submit the form
    

    演示HTML页面加载到WebBrowser控件中:

    <html>
    <head>
        <title></title>
    </head>
    <body>
        <form method="post" action="">
            <input type="text" name="testInput" value="test"/>
            <input type="submit" value="submit"/>
        </form>
    </body>
    </html>
    

    【讨论】:

    • 当你设置SetAttribute方法时,你是在设置“testInput”的值吗?如果是这样,您的代码如何引用该输入标签?
    • @CocoaDev 是的。我用 form.children[0] 引用该标签(form 元素的 Children 数组包含&lt;form&gt;&lt;/form&gt; 之间的所有子代。testInput 是第一个子代的原因form.children[**0**]).
    【解决方案2】:

    假设您正在将网页加载到 WinForm 应用程序上的 WebBrowser 控件中,您应该能够通过 WebBrowser.HtmlDocument.DomDocument 属性访问该文档。这是通过 MSHTML.IHTMLDocument2 接口对页面的 IE DOM 的非托管引用。

    【讨论】:

      【解决方案3】:

      使用WebClient下载页面,使用HtmlAgilityPack解析。

      一个例子:

      using (var wc = new WebClient())
      {
          var page = wc.DownloadString(url);
      
          HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
          doc.LoadHtml(page);
      
          //XPath
          var title = doc.DocumentNode.SelectSingleNode("//title").InnerText;
          var text = doc.DocumentNode.SelectSingleNode("//div[@id='readInner']")
                        .InnerText;
          //Linq
          var text = doc.DocumentNode.Descendants("div")
                      .Where(n => n.Attributes["id"].Value == "readInner")
                      .First()
                      .InnerText;
      }
      

      【讨论】:

      • 看起来很整洁,Linq to Objects 也可以在 Beta 中查询。
      【解决方案4】:

      使用 MSHTML.Dll 和 SHDocVw.dll

      我只是粘贴代码,将代码从 winform 传输到 IE 浏览器,您只需单击按钮数据将传输到网页,但在网页上和您的 Html 页面上的控件相同

      private SHDocVw.InternetExplorer TargetIE = null;
              string url;
              public Form1()
              {
                  InitializeComponent();
              }
      
              private void button1_Click(object sender, EventArgs e)
              {
                  GetTheIEObjectFromSystem);
                  SendTextToActiveElementWithSubmitOptionSet();
              }
              private void GetTheIEObjectFromSystem(
              {
                  SHDocVw.ShellWindows SWs = new SHDocVw.ShellWindows();
                  foreach (SHDocVw.InternetExplorer internetExplorer in SWs)
                  {
                      url = internetExplorer.LocationURL;
                      TargetIE = internetExplorer;
                      return;
                  }
      
              }
              private void SendTextToActiveElementWithSubmitOptionSet()
              {
                  mshtml.IHTMLDocument2 document = null;
                  document = TargetIE.Document as mshtml.IHTMLDocument2;
                  if (!document.activeElement.isTextEdit)
                  {
                      MessageBox.Show("Active element is not a text-input system");
                  }
                  else
                  {
                      HTMLInputElement HTMLI;
                      HTMLI = document.activeElement as HTMLInputElement;
                      var tag = HTMLI.document as mshtml.HTMLDocumentClass;
                      mshtml.IHTMLElementCollection hTMLElementCollection = tag.getElementsByTagName("input");
                      //for (int i = 0; i < a.length; i++)
                      {
                          foreach (mshtml.HTMLInputElement el in hTMLElementCollection)
                          {
                              switch (el.id)
                              {
                                  case "txtFirstName":
                                      el.value = textBox1.Text;
                                      break;
                                  case "txtLastName":
                                      el.value = textBox2.Text;
                                      break;
                                  case "txtAddress":
                                      el.value = textBox3.Text;
                                      break;
                                  case "txtMobile":
                                      el.value = textBox4.Text;
                                      break;
                              }                        
                          }
                      }
      
      
                  }
              }
      

      您可以根据需要进行更改,我相信它会起作用

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-18
        • 2016-09-05
        • 2021-02-02
        • 1970-01-01
        • 2012-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多