用下面的代码可以实现,把指定的网站加载到WebBrowse控件中,
然后向文本框填充数据,并且可以通过找Button链接的坐标,
用mouse_evetn事件提交表单。如果调整屏幕分辨率或换别的电脑,找坐标可能不准确,
是否可以通过getElementByTagName找标签,然后触发onclick?请大家帮忙看下,谢谢// WebTestDlg.h : header file
class CWebTestDlg : public CDialog
{
CWebBrowser2    m_ctrlWeb;
};// WebTestDlg.cpp : implementation file
//引用HTML相关头文件
#include <atlbase.h>
CComModule _Module;    
#include <mshtml.h>
#include <atlcom.h>#include <string>
using   namespace   std;//声明自动提交表单的函数
void PutFormValue(IHTMLDocument2 * pIHTMLDocument2);BOOL CWebTestDlg::OnInitDialog()
{
//默认打开移动宽带网站
m_ctrlWeb.Navigate("http://221.178.143.198:7001/style/default/default.jsp",NULL,NULL,NULL,NULL);
return TRUE;  // return TRUE  unless you set the focus to a control
}//实现自动提交表单
void PutFormValue(IHTMLDocument2 * pIHTMLDocument2)
{
    if(!pIHTMLDocument2)    
        return;
    
    HRESULT hr;
    CComBSTR bstrTitle;
    
    //获取加载页面的标题
    pIHTMLDocument2->get_title( &bstrTitle );    
    USES_CONVERSION;
    
    CComQIPtr<IHTMLElementCollection>spElementCollection;    
    hr = pIHTMLDocument2->get_forms( &spElementCollection );
    if (FAILED(hr))
    {
        AfxTrace(_T("获取表单的集合 IHTMLElementCollection 错误"));
        return;
    }
    
    long nFormCount=0;
    
    //获取表单数目
    hr = spElementCollection->get_length( &nFormCount );
    if ( FAILED( hr ) )
    {
        AfxTrace( _T("获取表单数目错误"));
        return;
    }
    
    for(long i=0; i<nFormCount; i++)
    {
        IDispatch *pDisp = NULL;    //取得第 i 项表单
        hr = spElementCollection->item( CComVariant( i ), CComVariant(), &pDisp );
        if ( FAILED( hr ) )        continue;
        
        CComQIPtr< IHTMLFormElement > spFormElement = pDisp;
        pDisp->Release();
        
        long nElemCount=0;            //取得表单中 域的数目
        hr = spFormElement->get_length( &nElemCount );
        if ( FAILED( hr ) )        continue;    
        
        CString strName;
        CString strVal;
        for(long j=0; j<nElemCount; j++)
        {
            CComDispatchDriver spInputElement;    //取得第 j 项表单域
            
            hr = spFormElement->item( CComVariant( j ), CComVariant(), &spInputElement );
            if ( FAILED( hr ) )    continue;
            
            CComVariant vName,vVal,vType;        //取得表单域的 名,值,类型
            hr = spInputElement.GetPropertyByName( L"name", &vName );
            if( FAILED( hr ) )    continue;
            hr = spInputElement.GetPropertyByName( L"value", &vVal );
            if( FAILED( hr ) )    continue;
            hr = spInputElement.GetPropertyByName( L"type", &vType );
            if( FAILED( hr ) )    continue;                    
            
            LPCTSTR lpName = vName.bstrVal?
                OLE2CT( vName.bstrVal ) : _T("NULL");    //未知域名
            LPCTSTR lpVal  = vVal.bstrVal?
                OLE2CT( vVal.bstrVal  ) : _T("NULL");    //空值,未输入
            LPCTSTR lpType = vType.bstrVal?
                OLE2CT( vType.bstrVal ) : _T("NULL");    //未知类型
            
            strName =  lpName;
            strName.TrimLeft();
            strName.TrimRight();
            
            //向用户名文本框内填充数据
            if (strName == "UserName")
            {
                TCHAR szText[32] = "123456";
                CComVariant vMyVal = (LPCTSTR)(szText);
                spInputElement.PutPropertyByName( L"value",&vMyVal);
            }
            
            //向密码文本框内填充数据
            if (strName == "PassWord")
            {
                TCHAR szText[32] = "789";
                CComVariant vMyVal = (LPCTSTR)(szText);
                spInputElement.PutPropertyByName( L"value",&vMyVal);
            }
        }  //for(long j=0; j<nElemCount; j++)
        
        //提交表单,感觉象是按了F5刷新
        //页面没跳转到提示用户名和密码错误的页面
        //spFormElement->submit();
    }   //for(long i=0; i<nFormCount; i++)
}//测试自动提交表单
void CWebTestDlg::OnButton1() 
{
    //创建IHTMLDocument2类型的对象
    CComPtr <IDispatch> spDispDoc;
    spDispDoc = m_ctrlWeb.GetDocument();    
    CComQIPtr<IHTMLDocument2> spDocument2 = spDispDoc;
    if (!spDocument2)        
        return;    
    
    PutFormValue(spDocument2);   //模拟鼠标单击
::SetCursorPos(350,420);
Sleep(500);
::mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
Sleep(500);
::mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
    Sleep(100);  
}

解决方案 »

  1.   

    之前发的帖子页面加载到WebBrowse中无法提交表单
    http://topic.csdn.net/u/20101102/20/9a9fa76a-2d14-4ca3-94f4-4f9020667386.html
    //移动宽带连接的网站
    http://221.178.143.198:7001/style/default/default.jsp<tr>
    <td height="50" colspan="2" align="center">
    <div class="log">
    <a href="#" onclick="onCheck()">登 录</a>
    </div>
    </td>
    </tr>
      

  2.   

    怎样能使表单元素的cnclick()事件发生
    http://topic.csdn.net/u/20080711/23/73dbe568-1cef-44a1-893b-4722ccfd71d8.html
      

  3.   


    ----------------
    换别的电脑,
    或调整屏幕分辨率,坐标获取就不准确啦,
    还有就是,如果把把WebBrowse所在的窗体隐藏,
    也会影响程序的运行
      

  4.   

    这确实是个问题
    不过对于操作web控件本身的方法我就不太熟悉了
      

  5.   

    spInputElement.GetPropertyByName,
    获取不到这个连接按纽,因为没有 name值。
      

  6.   

    VC6 是否支持IHtmlElement2接口呢???
      

  7.   

    没有id, name, value属性的 Hyer Link链接Button,
    听说可以用GetElementByTagName来获取一个对象(假设是A),
    然后A.Click(),就可以模拟鼠标单击;VC6下好象不支持IHtmlElement2,IHtmlElement3,IHtmlElement4
    等接口...