如何在桌面应用程序中使用C#将文本设置为WebBrowser控件中的输入

如何在桌面应用程序中使用C#将文本设置为WebBrowser控件中的输入,c#,.net,winforms,webbrowser-control,C#,.net,Winforms,Webbrowser Control,我正在使用C#在VS中开发一个桌面应用程序,该应用程序包含一个用于加载两列文件的按钮和一个用于加载网页的区域。。所以,我想把第一列放在一个特定的输入中,第二列放在网页的另一个输入中,然后单击网页中的一个按钮 有什么想法吗 提前谢谢 我找到了设置值的解决方案: HtmlElement button = webBrowser1.Document.GetElementById("btnC"); button.InvokeMember("click"); HtmlElement txt = webBr

我正在使用C#在VS中开发一个桌面应用程序,该应用程序包含一个用于加载两列文件的按钮和一个用于加载网页的区域。。所以,我想把第一列放在一个特定的输入中,第二列放在网页的另一个输入中,然后单击网页中的一个按钮

有什么想法吗


提前谢谢

我找到了设置值的解决方案:

HtmlElement button = webBrowser1.Document.GetElementById("btnC");
button.InvokeMember("click");

HtmlElement txt = webBrowser1.Document.GetElementById("txt1");
txt.SetAttribute("value","this is just an example");

使用此示例源代码。必要时添加错误处理

要使用IHTML*接口,请在项目中添加对Microsoft HTML对象库的引用

private void button1_Click(object sender, EventArgs e)
{

    string documentSource = @"
        <html>
        <body>
        <form action=""https://www.google.com/search"">
            <input name=""q"" id=""searchInput""/>
            <input type=""submit"" id=""searchButton""/> 
        </form>
        </body>
        </html>
    ";

    // navigate to document
    webBrowser1.Navigate("about:" + documentSource);

    // Wait until the document is fully loaded
    // Instead of using this loop, use "DocumentCompleted" event of the Webbrowser in
    // production code
    while (webBrowser1.ReadyState < WebBrowserReadyState.Complete)
    {
        Application.DoEvents();
    }

    // Get IHTMLDocument reference
    IHTMLDocument2 document = webBrowser1.Document.DomDocument as IHTMLDocument2;

    // Get searchInput and set value
    var searchInput = document.all.item("searchInput") as IHTMLInputElement;
    searchInput.value = "Hello";

    // Get searchButton and call click
    var searchButton = document.all.item("searchButton") as IHTMLElement;
    searchButton.click();
}
private void按钮1\u单击(对象发送者,事件参数e)
{
字符串documentSource=@“
";
//导航到文档
webBrowser1.导航(“关于:”+documentSource);
//等待文档完全加载
//在中使用Webbrowser的“DocumentCompleted”事件,而不是使用此循环
//生产代码
while(webBrowser1.ReadyState
可能与@NineBerry重复,没有它;不是