c#中用axWebBrowser中提交表单,或点击某个对象

来源:互联网 发布:分子动力学 软件ms 编辑:程序博客网 时间:2024/05/17 06:59

test.htm

<form id="form1">
<INPUT language=javascript class=button id=Btton style="BORDER-TOP-STYLE: groove; BORDER-RIGHT-STYLE: groove; BORDER-LEFT-STYLE: groove; BORDER-BOTTOM-STYLE: groove" onclick="if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); " type=submit value="Start" onclick="alert('dd')" name=Btton>
</form>

<input type="button" onclick="alert('test')" id="button1">

-----------------------------------------------------------------------------------

form1.cs

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using mshtml;

namespace WindowsApplication2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>

public class Form1 : System.Windows.Forms.Form
{
 
private AxSHDocVw.AxWebBrowser axWebBrowser1;
 
private System.Windows.Forms.Button button1;
 
private System.Windows.Forms.Button button2;
 
/// <summary>
 
/// 必需的设计器变量。
 
/// </summary>

  private System.ComponentModel.Container components = null;

 
public Form1()
 
{
  
//
  
// Windows 窗体设计器支持所必需的
  
//
   InitializeComponent();

  
//
  
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  
//
  }


 
/// <summary>
 
/// 清理所有正在使用的资源。
 
/// </summary>

  protected override void Dispose( bool disposing )
 
{
  
if( disposing )
  
{
   
if (components != null)
   
{
     components.Dispose();
    }

   }

  
base.Dispose( disposing );
  }


 
Windows 窗体设计器生成的代码

 
/// <summary>
 
/// 应用程序的主入口点。
 
/// </summary>

  [STAThread]
 
static void Main()
 
{
   Application.Run(
new Form1());
  }


 
private void button1_Click(object sender, System.EventArgs e)
 
{
  
object x = System.Reflection.Missing.Value;
   axWebBrowser1.Navigate(
@"E:/Documents and Settings/Administrator/桌面/test.htm", ref x, ref x, ref x, ref x);
  }


 
private void button2_Click(object sender, System.EventArgs e)
 
{
   mshtml.IHTMLDocument2 doc
= (mshtml.IHTMLDocument2)axWebBrowser1.Document;
   mshtml.IHTMLFormElement f1
=(mshtml.IHTMLFormElement)doc.all.item ("form1",0);
   ((mshtml.IHTMLFormElement)(f1)).submit();
   mshtml.IHTMLElement Element
= (mshtml.IHTMLElement)doc.all.item("button1", 0);
   Element.click();
  }

}

}


附:如何模拟IE中的表单提交?
1. 如果使用的是WebBrowser控件:
   你可以访问整个HTML DOM,设置input value, 然后submit:
   mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)webbrowser.Document;
   mshtml.IHTMLInputElement ele = (mshtml.IHTMLInputElement)doc.all.item("name", 0);
   ele.value = "user";
   ...
   mshtml.IHTMLFormElement ele = (mshtml.IHTMLFormElement)doc.all.item("myform", 0);
   ele.submit();

2. 如果没有使用WebBrowser,可以直接用WebRequest:
         WebRequest req = WebRequest.Create(url);
         req.Method = "POST";
         req.ContentType = "application/x-www-form-urlencoded";
         Stream stream = req.GetRequestStream();
         stream.Write(bytes, 0, bytes.Length);
         //bytes是byte[]形式的request字符串,如name=user&password=helloworld。

原创粉丝点击