首页 新闻 赞助 找找看

鼠标模拟点击webBrowser中元素的坐标

1
悬赏园豆:5 [已解决问题] 解决于 2012-01-17 10:11
 1 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;
9
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 }

请问代码中的

int x = 100和
int y = 80的值是怎么确定的,就是我想模拟点击网页中的一个元素,要怎么确定元素的坐标? 麻烦说的详细点。
阿什顿发斯蒂芬的主页 阿什顿发斯蒂芬 | 初学一级 | 园豆:101
提问于:2011-12-18 02:37
< >
分享
最佳答案
0

通过dom找到这个对象(网页元素),调用它本身的.click()方法不就行了吗?

收获园豆:5
三阶 | 小虾三级 |园豆:1436 | 2011-12-19 22:05

自动化查看

沧海一滴 | 园豆:211 (菜鸟二级) | 2013-09-12 10:22

我遇到一个点击对象(网页元素),但单击不生效的情况,大侠看看是怎么回事

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- EXT -->
<link rel="stylesheet" type="text/css" href="ext4.0/resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="ext4.0/shared/example.css" />
<script type="text/javascript" src="common/js/common.js"></script>
<script type="text/javascript" src="ext4.0/bootstrap.js"></script>

<script type="text/javascript" src="common/jquery/jquery-1.3.2.min.js"></script>

<title>自动化查看</title>
<style type="text/css">
       .icon-grid {
            background-image:url(ext4.0/shared/icons/fam/grid.png) !important;
        }
        .icon-clear-group {
            background-image:url(ext4.0/shared/icons/fam/control_rewind.png) !important;
        }
        
        .progress-out{width:300px;height:20px;background:#EEE;}
        .progress-in{width:0px; height:20px;background:#AAA;color:white;text-align:center;}
</style>
<script type="text/javascript">
    <!--//
    jQuery.noConflict();
    Ext.Loader.setConfig({
        enabled: true
    });
    Ext.Loader.setPath('Ext.ux', 'ext4.0/ux');
    Ext.onReady(function(){
        
          //JOB 模型
          Ext.define('Job', {
                extend: 'Ext.data.Model',
                fields: ['id', 'jobName','statFlag','stepList']
          });
          
          function statFlag(val,meta,model,rowIndex,colIndex) {
                if (val == 0) {
                    return '<span style="color:#gray;">未运行</span>';
                } else if (val == 1) {
                    return '<span style="color:#9AD936;">正在执行</span>';
                } else if (val == 2) {
                    return '<span style="color:red;">运行出错</span>';
                } else if (val == 3) {
                    return '<span style="color:red;">告警一次</span>';
                }
                return val;
          };
          
          function stepFlag(val,meta,model,rowIndex,colIndex) {
                if(val == null || val == ''){
                    val = '<span style="color:#gray;">未运行</span>';
                }
                return val;
          };
          
      
        Ext.create('Ext.container.Viewport', {
            layout: 'border',
            padding:2,
            items: [{
                region: 'center',
                layout:'border',
                border:false,
                flex:2, 
                items:[
                    {
                        region:'center',
                        title:'总数据',
                        frame:true, 
                        xtype:'grid',
                        id:'jobGrid',
                        columns: [{
                            text: 'ID',
                            flex: 1,
                            dataIndex: 'id'
                        },{
                            text: '名称',
                            flex: 1,
                            dataIndex: 'jobName'
                        },{
                            text: '运行状态',
                            flex: 1,
                            renderer:statFlag,
                            dataIndex: 'statFlag'
                        }],
                        store:Ext.create('Ext.data.Store', {
                        //    autoLoad:true,
                            model: 'Job',
                            data:{'total':[
                                           {"id":"100", "jobName":"id100", "statFlag":"0",stepList:[{"id":"100step1","jobId":"100","stepName":"100step1","moniContent":null},{"id":"100step2","jobId":"100","stepName":"100step1","moniContent":null}]},

                                           {"id":"101", "jobName":"id101", "statFlag":"0",stepList:[{"id":"101step1","jobId":"101","stepName":"101step1","moniContent":null},{"id":"101step2","jobId":"101","stepName":"101step1","moniContent":null}]},

                                           {"id":"102", "jobName":"id102", "statFlag":"0",stepList:[{"id":"102step1","jobId":"102","stepName":"102step1","moniContent":null},{"id":"102step2","jobId":"102","stepName":"102step1","moniContent":null}]},                        

                                           {"id":"103", "jobName":"id103", "statFlag":"0",stepList:[{"id":"103step1","jobId":"103","stepName":"103step1","moniContent":null},{"id":"103step2","jobId":"103","stepName":"103step1","moniContent":null}]}            
                                       ]},
                            
                            proxy: {
                                type: 'memory',
                                reader: {
                                    type: 'json',
                                    root: 'total'
                                }
                            }
                        }),
                        listeners:{
                            'selectionchange':function(_this,selected,eOpts){
                                if(selected.length > 0){
                                    Ext.data.StoreManager.lookup('stepStore').loadData(selected[0].get('stepList'))
                                }
                            }
                        }
                       
                    }
                ]
            }, {
                region: 'east',
                border:false,
                hideCollapseTool:true,
                collapsible: true,
                split: true,
                flex:1,
                layout:'border',
                items:[
                    {
                        region:'center',
                        flex:1,
                        title:'步骤列表',
                        frame:true,
                        xtype:'grid',
                        columns: [
                              { header: 'ID',  dataIndex: 'id',flex:1},
                              { header: '步骤', dataIndex: 'stepName', flex: 2 },
                              { header: '状态', dataIndex: 'moniContent',flex: 2,renderer:stepFlag}
                        ],
                        store:Ext.create('Ext.data.Store', {
                            storeId:'stepStore',
                            fields:['id', 'stepName', 'moniContent','stepKey'],
                            data:[],
                            proxy: {
                                type: 'memory',
                                reader: {
                                    type: 'json'
                                }
                            }
                        })
                    }      
                ]
            }]
        });        
        
    });
    
    
    
      
    
    
    //-->

</script>
</head>
<body>


</body>
</html>

这里不能上传附件,大侠如果需要demo。我发你邮箱

沧海一滴 | 园豆:211 (菜鸟二级) | 2013-09-12 10:24
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册