Call Click on an element you look up by ID. No pointer coordinates, browser, or JavaScript.
dotnet add package RecursiveAI.OneRender.ImageThis package pulls in InteractivePage from RecursiveAI.OneRender.
Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
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 and dotnet run.
The checkbox starts unchecked. After the click, IsChecked prints:
TrueOneRender 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:
page.Events.AddEventListener( EventTarget.Of(box), "click", e => e.PreventDefault());
Related recipes
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.