Skip to content

Connect to Moli browser

You want to drive a local Moli browser from C#. Use Puppeteer.ConnectAsync to attach to the running browser. In this example, Puppeteer Sharp waits for an order status to appear, clicks Confirm order, and reads back the updated status.

The example was tested on an Apple Silicon Mac with Puppeteer Sharp 25.18.1, Moli 1.1.28, and the .NET 10 SDK. Install Moli 1.1.28 and the .NET 10 SDK. The local test page uses Python 3’s built-in HTTP server.

Create a console project. Run the remaining commands from the MoliOrder directory.

Terminal window
dotnet new console --framework net10.0 --name MoliOrder
cd MoliOrder
dotnet add package PuppeteerSharp --version 25.18.1
mkdir web

Save this page as web/index.html. The order status is initially empty.

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Order lookup</title>
<script src="/order.js" defer></script>
</head>
<body>
<h1>Order lookup</h1>
<button id="confirm" type="button">Confirm order</button>
<div id="result"></div>
</body>
</html>

Save this script as web/order.js. It adds the status after the page loads and changes it when the button receives a click.

const result = document.querySelector("#result");
const button = document.querySelector("#confirm");
setTimeout(() => {
const summary = document.createElement("p");
summary.id = "summary";
summary.textContent = "Order 42 is ready";
result.appendChild(summary);
}, 100);
button.addEventListener("click", () => {
result.querySelector("#summary").textContent = "Order 42 confirmed";
});

Start the web server and leave it running.

Terminal window
python3 -m http.server 8000 --bind 127.0.0.1 --directory web

In a second terminal, start Moli with --realtime --resource. In Moli 1.1.28, --realtime enables experimental real-time layout. It keeps element positions up to date as the page changes, so Puppeteer Sharp can click the button at its current position. The --resource flag loads optional assets such as images and fonts, which can affect layout on the pages you test.

Terminal window
moli serve --host 127.0.0.1 --port 9222 --realtime --resource

Replace Program.cs in the console project with the following code. BrowserURL points to Moli on port 9222. If that port is already in use, choose a free port in both the Moli command and BrowserURL. The address passed to GoToAsync is the test page on port 8000.

using PuppeteerSharp;
var browser = await Puppeteer.ConnectAsync(new ConnectOptions
{
BrowserURL = "http://127.0.0.1:9222"
});
try
{
await using var page = await browser.NewPageAsync();
await page.GoToAsync("http://127.0.0.1:8000/", new NavigationOptions
{
WaitUntil = new[] { WaitUntilNavigation.DOMContentLoaded }
});
await page.WaitForSelectorAsync("#summary");
const string readStatus = "document.querySelector('#summary').textContent";
Console.WriteLine(await page.EvaluateExpressionAsync<string>(readStatus));
await page.ClickAsync("#confirm");
Console.WriteLine(await page.EvaluateExpressionAsync<string>(readStatus));
}
finally
{
browser.Disconnect();
}

Run the program in a third terminal from MoliOrder.

Terminal window
dotnet run

The program prints two lines.

Order 42 is ready
Order 42 confirmed

The first line comes from JavaScript that ran after navigation. The second comes from the button’s click handler. The program closes its page and disconnects. Press Ctrl+C in the two server terminals when you are done.

Made with love by Dario Kondratiuk