当前位置:编程学习 > C#/ASP.NET >>

另开一个加分贴,webbrowser自动提交模拟鼠标的问题

有一个网页,尝试自动提交,那个元素的HTML为” <input id="password" autocomplete="off" class="textBoxInfo" type="password" data-bind="value: customer.password" onkeyup="return Fanex.Customer.CheckPassword(this, 'spnNewPwdIcon');" maxlength="10" style="position: relative;"> 
                                “

简单  ((DispHTMLInputElement)all.item("password", 0)).value =“password123”,这样不行,JS报错,提示调用出错,我看了下,好像是onkeyup动作的JS函数生成的一些东西,如果用鼠标点一下,手动输入就可以,那如何模拟这个鼠标的动作呢,在这个网页上? --------------------编程问答-------------------- 你这半天没人回答,我也不知道你想问什么,以前我做过一个qq控件自动登陆的,不知道对你有没有帮助。
HtmlElement  u=webBrowser1.Document .All [“password"];//获取你需要的控件
 u.SetAttribute("value","password123");//设置password的value
 u.InvokeMember("click");//触发passwrod的click事件

自己慢慢研究吧,就帮你这么多了 --------------------编程问答-------------------- webbrowser我没干过,我干过直接操作IE,处理方式是将item拿到后转换为InputHTMLElement,然后调用这个对象的click()、focus()等等js都有的方法 --------------------编程问答-------------------- refer: 
 private void Form1_Load(object sender, EventArgs e)
        {
            this.webBrowser1.Url = new Uri("http://www.baidu.com");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.webBrowser1.ReadyState == WebBrowserReadyState.Complete)
            {
                HtmlElement text = this.webBrowser1.Document.All["kw"];
                HtmlElement button = this.webBrowser1.Document.All["su"];
                text.SetAttribute("value", "guwei4037");
                button.InvokeMember("click");
            }
        }
--------------------编程问答--------------------   楼上的也可以,如果非要用mshtml 的话如下:

     hr1=pDisp->QueryInterface(IID_IHTMLDocument2,(void**)&pDocument);
hr=pDocument->get_all(&pCollection);
       VARIANT varIndex, var;  
            for ( int i = 0; i <celem; i++ )  
            {  
                varIndex.vt = VT_UINT;  
                varIndex.lVal = i;  
                VariantInit(&var);  
                hr = pCollection->item(varIndex,var,&pDisp);
                if ( hr == S_OK )  
                {   
                    IHTMLElement *pElement; 
     IHTMLScriptElement *pScriptElement;
                    //hr = pDisp->QueryInterface(IID_IHTMLScriptElement,(void**)&pScriptElement); 
  hr = pDisp->QueryInterface(IID_IHTMLElement,(void**)&pElement);
 
                    pElement->get_innerText(&bstr);//这句赋值就行了 --------------------编程问答--------------------  private void Form1_Load(object sender, EventArgs e)
        {
            this.webBrowser1.Url = new Uri("http://www.baidu.com");
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            if (this.webBrowser1.ReadyState == WebBrowserReadyState.Complete)
            {
                HtmlElement text = this.webBrowser1.Document.All["kw"];
                HtmlElement button = this.webBrowser1.Document.All["su"];
                text.SetAttribute("value", "guwei4037");
                button.InvokeMember("click");
            }
        }
这个不错,不过最好TRY一下.ALL这样不是好确定的.你要保证你网页加载完成后才OK. --------------------编程问答-------------------- if (this.webBrowser1.ReadyState == WebBrowserReadyState.Complete)
有个ISBUSY加上就更好了. --------------------编程问答-------------------- 回复:sunyaxingqq
  这样U为NULL,等多久也是,好像是找不到。还有其它的回贴者,看清楚,基本的赋值操作我都会,我要的模拟鼠标按下弹起这样子的。 --------------------编程问答--------------------
   IHTMLElement i=(IHTMLElement)all.item("password", 0);
                                i.click();
                                if (((DispHTMLInputElement)all.item("password", 0)).value == null)
                                    ((DispHTMLInputElement)all.item("password", 0)).value = =“password123”;


这样没有效果,不一定是在ONKEYUP里记录的,好像没有跟踪到,可能是在样式或者BING里生成的JS,确还是根据鼠标动作形成的。大家给点思路 --------------------编程问答-------------------- 你要明白提交表单原理就不会那么做了  看这个http://download.csdn.net/detail/zhuankeshumo/5656679 --------------------编程问答-------------------- webBrowser可以执行脚本(而并不需要文档中写了这些脚本)。

你可以先执行脚本 Fanex.Customer.CheckPassword(password, 'spnNewPwdIcon');

然后再复制。 --------------------编程问答-------------------- 然后再复制  -->  然后再赋值

而且 onkyeup 可不是鼠标操作,而是键盘按键抬起操作。 --------------------编程问答-------------------- form1.submit(); --------------------编程问答-------------------- 除 --------------------编程问答-------------------- 好像不是KEYUP的关系。可能是data-bind="value: customer.password" 的关系,我不明白这是什么意思,
调用了一个websource.axd?hhh里的JS, 好像是一个专门处理事件的东东,这个怎么执行JS
 showDown: function (elementInput, message, xPos, pointerXPos, yPos, pointerYPos, height, timeout) {
        // <span class="fHintDown"><span>This is the message.<span><span class="hint-pointer"> </span></span>
        var hint = document.getElementById('fHintDown');
        var hintPointer = null;
        if (hint == null) {
            var hint = document.createElement("SPAN");
            hint.className = 'hint';
            var hintMsg = document.createElement("SPAN");
            hint.appendChild(hintMsg);
            hintPointer = document.createElement("SPAN");
            hintPointer.className = 'hint-pointer-down';
            hint.appendChild(hintPointer);
            hint.id = 'fHintDown';
            hint.style.top = '10px';
            hint.style.left = '10px';
            hint.style.zIndex = 999999;
            hint.onclick = function () { // Hide onclick
                hint.style.display = 'none';
            };
            document.body.appendChild(hint);
        };
        hintPointer = hint.lastChild;
        hint.firstChild.innerHTML = message;
        if (this.timeOutHideTooltip) {
            clearTimeout(this.timeOutHideTooltip);
            this.timeOutHideTooltip = null;
        };
        if (typeof (timeout) == 'undefined') {
            timeout = 1000;
        }
        this.timeOutHideTooltip = setTimeout(function () {
            if (hint == null) {
                return;
            }
            hint.style.display = 'none';
        }, timeout); // Auto hide in 1s
        if (elementInput != null) {
            if (typeof xPos == 'undefined') {
                xPos = 0;
            }
            if (typeof pointerXPos == 'undefined') {
                pointerXPos = 10;
            }
            if (typeof yPos == 'undefined') {
                yPos = -45;
            }
            if (typeof (pointerYPos) == 'undefined') {
                pointerYPos = 38;
            }
            if (typeof (height) == 'undefined') {
                height = 18;
            }
            hint.style.height = height + 'px';
            hintPointer.style.left = (pointerXPos + 'px');
            hintPointer.style.top = (pointerYPos + 'px');
            var pos = Validators.utils.findPosRelativeToViewport(elementInput);
            hint.style.left = (pos[0] + xPos) + 'px';
            hint.style.top = (pos[1] + yPos) + 'px';
            elementInput.onchange = function () {
                hint.style.display = 'none';
            };
            elementInput.focus();
        };
        hint.style.display = 'block';
    }
--------------------编程问答--------------------
using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 using System.Runtime.InteropServices;

10 namespace BrowserMouseClick
11 {
12     public partial class Form1 : Form
13     {
14         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
15         static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
16 
17         [DllImport("user32.dll", SetLastError = true)]
18         static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
19 
20         [DllImport("user32.dll", CharSet = CharSet.Auto)]
21         static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
22 
23         public Form1()
24         {
25             InitializeComponent();
26         }
27 
28         private void Form1_Load(object sender, EventArgs e)
29         {
30             webBrowser1.Navigate("http://www.devpub.com");
31         }
32 
33         private void btnMouseClick_Click(object sender, EventArgs e)
34         {
35             int x = 100; // X coordinate of the click 
36             int y = 80; // Y coordinate of the click 
37             IntPtr handle = webBrowser1.Handle;
38             StringBuilder className = new StringBuilder(100);
39             while (className.ToString() != "Internet Explorer_Server") // The class control for the browser 
40             {
41                 handle = GetWindow(handle, 5); // Get a handle to the child window 
42                 GetClassName(handle, className, className.Capacity);
43             }
44 
45             IntPtr lParam = (IntPtr)((y << 16) | x); // The coordinates 
46             IntPtr wParam = IntPtr.Zero; // Additional parameters for the click (e.g. Ctrl) 
47             const uint downCode = 0x201; // Left click down code 
48             const uint upCode = 0x202; // Left click up code 
49             SendMessage(handle, downCode, wParam, lParam); // Mouse button down 
50             SendMessage(handle, upCode, wParam, lParam); // Mouse button up 
51         }
52     }
53 }
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,