简体   繁体   English

以编程方式在Firefox中自动执行鼠标移动和点击

[英]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. 我正在尝试在C#中创建一个自动点击程序,以便更快地完成一些管理任务。 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. 简而言之,我将使用一个程序(Firefox),并且需要一个程序来自动移动鼠标并单击我告诉它的任何地方。

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. 我希望我的程序在Firefox上运行,一旦加载就立即开始点击我告诉它的位置。 It'll need to click thirteen 30x30 pixel buttons and will then stop, leaving me to finish the task. 它需要点击十三个30x30像素按钮然后停止,让我完成任务。

How would I go about such a task? 我怎么去做这样的任务?

EDIT: I forgot to mention that the front-end is in Flash. 编辑:我忘了提到前端是在Flash中。 Weird, I know. 很奇怪,我知道。 Either way, it means that programs like Selenium won't work as its tracks page movements and clicks on links. 无论哪种方式,这意味着像Selenium这样的程序将无法用作其跟踪页面移动和链接点击。

While this doesn't answer your question with C# code I would really have to recommend you look at AutoIt . 虽然这不能用C#代码回答你的问题,但我真的不得不建议你看看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. 我已经成功地将它用于各种各样的任务,从反复输入用于测试Web应用程序的表单数据到在MMO游戏中对抗怪物。

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. 您可以使用Selenium轻松完成此操作,包括使用为您生成C#代码的IDE(FireFox插件)记录您的操作。

To expand on @Chris Marisic's answer, you can actually use AutoIt from C# (or other languages) using AutoItX. 要扩展@Chris Marisic的答案,您实际上可以使用AutoItX从C#(或其他语言)中使用AutoIt。 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 我没试过这个,但显然你可以把它编译成DLL并从你的代码中调用它,参见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 http://www.codeproject.com/KB/system/globalmousekeyboardlib.aspx

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

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