C# – Automating Mouse Movement and Clicks in Firefox programmatically

automationc++

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 30×30 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.

Best Solution

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.

Related Question