简体   繁体   中英

Automating Mouse Movement and Clicks in Firefox programmatically

I am trying to create an automated clicker program in C# to make some administrative tasks quicker. In short, I will be using one program (Firefox) and will need a program to automatically move the mouse and click wherever I tell it to.

At the moment I have this code, although I think I'm pretty far away from an actual solution.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing;

namespace Clicker
{
    public partial class Form1 : Form
    {

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);

        private const int MOUSEEVENTF_LEFTDOWN = 0x02;
        private const int MOUSEEVENTF_LEFTUP = 0x04;
        private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
        private const int MOUSEEVENTF_RIGHTUP = 0x10;

        public void DoMouseClick()
        {
            //Call the imported function with the cursor's current position
            Cursor.Position = new Point((int)10, (int)10);

            mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, Cursor.Position.X, Cursor.Position.Y, 0, 0);
        }

        public Form1()
        {
            //InitializeComponent();
        }
    }
}

I want my program to run on top of Firefox and once it has loaded to immediately start clicking in the positions I tell it to. It'll need to click thirteen 30x30 pixel buttons and will then stop, leaving me to finish the task.

How would I go about such a task?

EDIT: I forgot to mention that the front-end is in Flash. Weird, I know. Either way, it means that programs like Selenium won't work as its tracks page movements and clicks on links.

While this doesn't answer your question with C# code I would really have to recommend you look at AutoIt . It is a very simple scripting language for creating defined tasks to be repeated. I've used it successfully for a wide range of tasks from repeatedly entering in form data for testing web applications to fighting monsters in a MMO game.

Does it need to actually automate the mouse, or just press the buttons? You could easily do this with Selenium , including recording your actions with the IDE (a FireFox addin) which produces the C# code for you.

To expand on @Chris Marisic's answer, you can actually use AutoIt from C# (or other languages) using AutoItX. I haven't tried this, but apparently you can compile it to a DLL and call it from your code, see http://www.autoitscript.com/forum/index.php?showtopic=39262

Here's a simple library that is pretty nifty:

http://www.codeproject.com/KB/system/globalmousekeyboardlib.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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