使用WebBrowser控件在C#网页形式提交 [英] Submission of a webpage form using WebBrowser control in C#

查看:193
本文介绍了使用WebBrowser控件在C#网页形式提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一般在网络上看到了很多关于这一特定主题帖子SO以及最如果不是所有的代码如下



<$ P $见过p> 私人无效btnSubmit_Click(对象发件人,RoutedEventArgs E)
{
WebBrowser1.Navigate时(新的URI(http://samples.msdn.microsoft.com/workshop/samples /author/dhtml/refs/onsubmit.htm));
}

私人无效btnLogin_Click(对象发件人,RoutedEventArgs E)
{
mshtml.HTMLDocument HTMLDOC = NULL;

HTMLDOC =(mshtml.HTMLDocument)this.webBrowser1.Document;

如果(webBrowser1.Document!= NULL)
{
的foreach(mshtml.HTMLFormElement在htmlDoc.forms形式)
{
form.submit( );
中断;
}
}
}



中的代码有没有任何错误但对于生活的不提交。我使用的示例页面有简单的按钮,它做什么,它就会提醒单选按钮的选择,然后提交表单。对于当表单通过代码使用WebBrowser控件,在表单提交,但该警报一直没有出现提交了一些奇怪的原因。



我不知道我在做什么错在这里。任何帮助,将不胜感激。


解决方案

会在按钮上进行点击你需要它做什么?您将需要添加一个COM参考Microsoft HTML对象库(您可能已经)。例如,如果你谷歌加载到WebBrowser控件,这个代码将会把世界你好到搜索框中进行搜索:

  mshtml.IHTMLDocument2 DOC =((mshtml.HTMLDocumentClass)webBrowser1.Document); 


((mshtml.IHTMLElement)doc.all.item(Q))的setAttribute(价值,世界你好)。
MessageBox.Show(点击手气不错按钮);
((mshtml.HTMLInputElement)doc.all.item(BTNI))点击()。



编辑:我更新了WPF WebBrowser控件用途组件的代码。另外请注意,这有时会引发谷歌从一个脚本错误,但似乎基于一些Ajax的来电谷歌已经在主页上是一个时间问题。


I have seen a lot of posts regarding this particular subject on SO as well as on the web in general and most if not all code is as seen below

private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
  webBrowser1.Navigate(new Uri("http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/onsubmit.htm"));
}

private void btnLogin_Click(object sender, RoutedEventArgs e)
{
    mshtml.HTMLDocument htmlDoc = null;

    htmlDoc = (mshtml.HTMLDocument) this.webBrowser1.Document;

    if (webBrowser1.Document != null)
    {
        foreach (mshtml.HTMLFormElement form in htmlDoc.forms)
        {
            form.submit();
            break;
        }
    }
}

The code has no errors whatsoever but for the life its not submitting. The sample page that I am using has simple button, what it does, it alerts the selection of the radio button and then submits the form. For some strange reason when the form is submitted via code using the WebBrowser control, the form is submitted but the alert never shows up.

I am not sure what I am doing wrong here. Any help on this would be appreciated.

解决方案

Would performing a click on the button do what you need it to do? You will need to add a COM reference to the Microsoft HTML Object Library (which you may already have). For example, if you load up google into the webbrowser control, this code will place "hello world" into the search box and perform the search:

        mshtml.IHTMLDocument2 doc = ((mshtml.HTMLDocumentClass)webBrowser1.Document);


         ((mshtml.IHTMLElement)doc.all.item("q")).setAttribute("value", "hello world");
         MessageBox.Show("Clicking I'm feeling lucky button");
        ((mshtml.HTMLInputElement)doc.all.item("btnI")).click();

Edit: I updated the code for the components that the WPF WebBrowser control uses. Also note that this sometimes throws a script error from google, but that appears to be a timing issue based on some of the ajax calls google has on the home page.

这篇关于使用WebBrowser控件在C#网页形式提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆