C#使用WebBrowser控件并需要访问DOM元素

C#使用WebBrowser控件并需要访问DOM元素,c#,winforms,internet-explorer,dom,C#,Winforms,Internet Explorer,Dom,我在WinForm应用程序中使用C加载了一个网页# 我需要通过编程将数据键入该页面上的特定字段(不使用WATIN) 如果有人有其他解决办法,我愿意接受 该页面没有AJAX或JavaScript。它们是简单的HTML数据输入表单。假设您正在将网页加载到WinForm应用程序的WebBrowser控件中,您应该能够通过WebBrowser.HtmlDocument.DomDocument属性访问文档。这是通过MSHTML.IHTMLDocument2界面对页面IE DOM的非托管引用。用于下载页面并

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

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

如果有人有其他解决办法,我愿意接受


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

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

用于下载页面并对其进行解析

例如:

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;
}

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

C#代码:

if(webBrowser1.Document==null)返回;
var form=webBrowser1.Document.Forms[0]//形式元素
var input=form.Children[0]//输入元素
SetAttribute(“值”、“输入值”)//设置输入值
表格.调用成员(“提交”)//提交表格
加载到
WebBrowser
控件中的演示HTML页面:


使用MSHTML.Dll和SHDocVw.Dll

我只是粘贴代码,将代码从winform转换到IE浏览器,在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;
                        }                        
                    }
                }


            }
        }
private SHDocVw.InternetExplorer TargetIE=null;
字符串url;
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
GetTheIEObjectFromSystem);
SendTextToActivieElementWithSubmitOptionSet();
}
private void GettheIEObject fromSystem(
{
SHDocVw.ShellWindows SWs=新的SHDocVw.ShellWindows();
foreach(SWs中的SHDocVw.InternetExplorer InternetExplorer)
{
url=internetExplorer.LocationURL;
TargetIE=internetExplorer;
返回;
}
}
private void SendTextToActivieLementWithSubmitOptionSet()
{
mshtml.IHTMLDocument2 document=null;
document=TargetIE.document为mshtml.IHTMLDocument2;
如果(!document.activeElement.isTextEdit)
{
Show(“活动元素不是文本输入系统”);
}
其他的
{
HTMLInputElement HTMLI;
HTMLI=document.activeElement作为HTMLInputElement;
var tag=HTMLI.document作为mshtml.HTMLDocumentClass;
mshtml.IHTMLElementCollection hTMLElementCollection=tag.getElementsByTagName(“输入”);
//for(int i=0;i

你可以随心所欲地进行更改,我确信它可以正常工作,看起来非常整洁,在Beta版中也可以查询对象。当你设置SetAttribute方法时,你是否设置了“testInput”的值?如果是,你的代码如何引用该输入标记?@cocooadev Yes。我使用表单引用该标记。children[0](
form
元素的Children数组包含
testInput
之间的所有子元素,因此
form.Children[**0**]
是第一个子元素)。