Tutorials

Tutorials

Clicking a Captcha with a GPALForm and Hardware Input

A small GPALForm with a single button kicks off an Application automation chain: open an app, find an image-matched captcha checkbox, and click it with simulated mouse movement. Shows how a desktop form can act as a manual trigger for a GPAL workflow.

Complete Program

Here's the whole workflow, start to finish. Each piece is broken down and explained below.

using System;

using System.Windows.Forms;

using GenerallyPositive;

using GenerallyPositive.Application;

using GenerallyPositive.GPALForm;

using static GenerallyPositive.GPALForm.GPALForm;

static GPALLabel titleLabel = (GPALLabel)GPAL.Label

.WithText("Captcha Clicker");

static Selector captchaSelector = (Selector)GPAL.Selector

.WithSimulateMouse(true)

.WithImage(@"C:imagescaptcha.jpg")

.WithImageOffsetX(20)

.WithImageOffsetY(10);

static GPALButton startButton = (GPALButton)GPAL.Button

.WithName("Start")

.WithText("Start The Workflow")

.WithCallback<EventHandler>(StartClickHandler)

.WithShortcutKey(Keys.Alt | Keys.S);

static GPALButton exitButton = (GPALButton)GPAL.Button

.WithName("Exit")

.WithText("Exit")

.WithCallback<EventHandler>(ExitClickHandler)

.WithShortcutKey(Keys.Alt | Keys.E);

GPAL.WithExceptionHandler(ExceptionEventHandler)

.WithInformationHandler(ExceptionEventHandler);

GenerallyPositive.GPALForm.GPALForm myForm = (GenerallyPositive.GPALForm.GPALForm)GPAL.Form

.WithTitle("Captcha Clicker")

.WithLeft(1500)

.WithTop(100)

.WithFormControl(titleLabel)

.WithFormControl(startButton)

.WithFormControl(exitButton)

.ShowDialog();

static void StartClickHandler(object sender, EventArgs e)

{

GenerallyPositive.Application.Application application = (GenerallyPositive.Application.Application)GPAL.Application

.Open("explorer.exe")

.WithSelector(captchaSelector)

.WaitFor(1_000)

.LeftClick();

}

static void ExitClickHandler(object sender, EventArgs e)

{

((Form)((Button)sender).Parent.Parent).Hide();

}

An Image-Matched, Mouse-Simulated Selector

The captcha checkbox isn't found by CSS or XPath - it's found by matching a saved screenshot. WithSimulateMouse(true) tells GPAL to move the cursor naturally to the matched location rather than dispatching a synthetic click event, which matters for captchas that watch for human-like input.

static Selector captchaSelector = (Selector)GPAL.Selector

.WithSimulateMouse(true)

.WithImage(@"C:imagescaptcha.jpg")

.WithImageOffsetX(20)

.WithImageOffsetY(10);

WARNING

Simulated mouse movement and hardware-level clicks act on real screen coordinates. If the target window loses focus or moves between WaitFor and LeftClick, the click can land on the wrong element entirely.

A Minimal GPALForm as a Trigger

GPAL.Form builds a small desktop window with a label and two buttons. WithCallback wires each button to a handler method, and WithShortcutKey gives each one an Alt-key accelerator. ShowDialog() blocks until the form closes, which is fine here since the form's only job is to wait for the user to press Start.

GenerallyPositive.GPALForm.GPALForm myForm = (GenerallyPositive.GPALForm.GPALForm)GPAL.Form

.WithTitle("Captcha Clicker")

.WithLeft(1500)

.WithTop(100)

.WithFormControl(titleLabel)

.WithFormControl(startButton)

.WithFormControl(exitButton)

.ShowDialog();

Open, Find, Click - The Application Chain

GPAL.Application.Open launches a desktop process by name. From there the chain looks just like a browser chain: WithSelector picks the target, WaitFor gives the app time to render, and LeftClick fires the click using whatever interaction mode the selector specifies (here, simulated mouse via the image selector).

static void StartClickHandler(object sender, EventArgs e)

{

GenerallyPositive.Application.Application application = (GenerallyPositive.Application.Application)GPAL.Application

.Open("explorer.exe")

.WithSelector(captchaSelector)

.WaitFor(1_000)

.LeftClick();

}

TIP

GPAL.Application uses the same Selector and chaining model as GPAL.Browser - WithSelector, WaitFor, LeftClick, and friends all behave the same way whether you're driving a browser tab or a desktop application window. See Desktop Application Automation.

Exit Hides the Form, Doesn't Close It

The exit handler walks up from the clicked button to the owning Form and calls Hide() rather than Close(). This keeps the form (and its event handlers) alive in memory, which matters if other code still expects to interact with it later in the same process.

static void ExitClickHandler(object sender, EventArgs e)

{

((Form)((Button)sender).Parent.Parent).Hide();

}