---
title: "Programmatically click a rendered HTML element in C#"
url: https://recursiveai.org/onerender/cookbook/activate-html-control-with-programmatic-click/
description: "Programmatically click a rendered HTML control in C# with OneRender. Toggle a checkbox by ID and see how canceling the click restores its state."
version: "1.5.0"
---

# Click a rendered HTML element in C#

**OneRender** is a free, open-source HTML and CSS rendering library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneRender.Image) · [GitHub](https://github.com/recursive-ai-oss/onerender-csharp) · [Docs](https://recursiveai.org/onerender/docs/) · [Download example (.zip)](https://recursiveai.org/onerender/cookbook/activate-html-control-with-programmatic-click/activate-html-control-with-programmatic-click.zip)

Call `Click` on an element you look up by ID. No pointer coordinates, browser, or JavaScript.

```shell
dotnet add package RecursiveAI.OneRender.Image
```

This package pulls in `InteractivePage` from `RecursiveAI.OneRender`.

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OneRender;
using OneRender.Image;
using OneRender.Interaction;

const string html = """
    <input id=alerts type=checkbox>
    """;
var fonts = FontProviders.CreateDefault();
var page = InteractivePage.Create(
    html, new HtmlPageOptions(fonts));
var box = page.FindElement("alerts");
page.Click(box, 0);
Console.WriteLine(page.IsChecked(box));
```

Paste the code above into Program.cs of a new `dotnet new console` project and `dotnet run`, or unzip [the example](https://recursiveai.org/onerender/cookbook/activate-html-control-with-programmatic-click/activate-html-control-with-programmatic-click.zip) and `dotnet run`.

The checkbox starts unchecked. After the click, `IsChecked` prints:

```text
True
```

OneRender doesn’t run the page’s `<script>` or `onclick` handlers; use `page.Events.AddEventListener` to attach C# listeners instead.

A disabled checkbox does nothing. With other markup, check the lookup result first: `FindElement` returns the null node if it finds neither the ID nor a form control with the given name.

## Variations

**Cancel activation in a listener.** Add this before `page.Click(box, 0)` in the example. The final checked state prints `False` instead of `True`:

```csharp
page.Events.AddEventListener(
    EventTarget.Of(box), "click",
    e => e.PreventDefault());
```

## Related recipes

- [Set a rendered radio button without clicking](https://recursiveai.org/onerender/cookbook/set-radio-button-selection-in-rendered-html/)
- [Hit-test rendered HTML and dispatch events](https://recursiveai.org/onerender/cookbook/hit-testing-events/)

## The short answer

Find an element with `InteractivePage.FindElement`, then pass that node and a timestamp in milliseconds to `InteractivePage.Click`. Use `IsChecked` to read the resulting state of a checkbox. The call dispatches a `click` to the element and runs its normal activation if the event isn’t canceled. In the sample, that activation checks the box; the return value of `IsChecked` shows the effect without needing to save or open an image.

## How do I click an HTML element by ID from C#?

Give the element an ID, create an `InteractivePage` from the HTML, and look it up with `FindElement`. The lookup first checks IDs in document order, then looks for an exact `name` match on a form control. Give controls distinct IDs so that a lookup names the one you intend to activate. The timestamp belongs to the host. `0` is sufficient for the one click in the example; in a host with its own input clock, pass that clock’s timestamp.

With this method you don’t need to scroll an element into view or compute its center. The engine sends the event to the node you named even if the node is covered by something else. Use it when the test is about what a specific control does, not whether a person could reach it on the screen.

## Does a programmatic click fire the same events as a mouse click?

It dispatches `click` through the normal event path, but doesn’t dispatch `pointerdown`, `mousedown`, `pointerup`, or `mouseup`, and doesn’t move focus. In the case of a checkbox or radio, the listener sees the changed state during the click. If the listener cancels the click, the engine restores the previous state, which is why the variation prints `False` after dispatch finishes.

If you need to verify that an overlay intercepts a pointer, use `HandlePointer` with a down and up at the coordinates you want to test. If you only want to set a checkable control’s state and don’t want click listeners to run, use `SetChecked` instead. Those are different tests: a pointer sequence checks which node gets hit, a named click checks activation, and setting state doesn’t test click behavior at all.
