简体   繁体   English

在不移动光标的情况下在浏览器中模拟鼠标单击

[英]Simulate Mouse Click in a browser without Moving Cursor

I created a windows form and added two buttons and a browser in the form. 我创建了一个Windows窗体,并在窗体中添加了两个按钮和一个浏览器。 I used a program to get the mouse coordinates and click a login button. 我使用了一个程序来获取鼠标坐标,然后单击一个登录按钮。 I would like to implement this without moving the cursor. 我想在不移动光标的情况下实现这一点。 I am not looking to find the login button ID or use HTML request/response. 我不想查找登录按钮ID或使用HTML请求/响应。

Any idea if it is possible to click non html button in a browser without moving the cursor. 任何想法,如果可以在浏览器中单击非html按钮而不移动光标。

This code below works but only for other controls within the form but not for the browser that is part of the form 下面的代码有效,但仅适用于表单中的其他控件,不适用于作为表单一部分的浏览器

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace simulateclicktest
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        private static extern IntPtr SendMessage(IntPtr hWnd, int Msg,
            IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", EntryPoint = "WindowFromPoint",
            CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr WindowFromPoint(Point point);

        private const int BM_CLICK = 0x00F5;

        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Specify the point you want to click
            var screenPoint = new Point(157
                ,455);
            // Get a handle
                                  var handle = WindowFromPoint(screenPoint);
            // Send the click message
                  if (handle != IntPtr.Zero)
            {
                SendMessage(handle, BM_CLICK, IntPtr.Zero, IntPtr.Zero);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("clicked");
        }

    }
}

Try find button ID and use this code: 尝试查找按钮ID并使用以下代码:

private void button1_Click(object sender, EventArgs e)
{
    webBrowser1.Document.GetElementById("button_ID").InvokeMember("click");
}

If you have webBrowser control on the form and know ID of button, DllImport is no needed. 如果您在表单上具有webBrowser控件并且知道按钮的ID,则不需要DllImport。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM