Open
The following example demonstrates how to request a browser instance, navigate to a webpage, and close the browser:
using BrowserService svc = new("YOUR CLOUDBROWSER.AI TOKEN");
//Request Cloud Browser AI to open a browser
//using default settings
var rp = await svc.Open().ConfigureAwait(false);
if (rp.Status == ResponseStatus.Succes) {
Console.WriteLine("Browser requested");
} else {
Console.WriteLine("Error requesting browser: {0}", rp.Status.ToString());
return;
}
var browser = await Puppeteer.ConnectAsync(new ConnectOptions {
BrowserWSEndpoint = rp.Address,
DefaultViewport = null,
AcceptInsecureCerts = true,
SlowMo = 0
}).ConfigureAwait(continueOnCapturedContext: false);
Console.WriteLine("Browser connected");
var page = (await browser.PagesAsync().ConfigureAwait(false))[0];
await page.GoToAsync("http://www.cloudbrowser.ai").ConfigureAwait(false);
Console.WriteLine("Web visited");
await Task.Delay(5000).ConfigureAwait(false);
//You can close the browser using puppetter or CloudBrowser AI api
//await svc.Close(rp.Address).ConfigureAwait(false);
await browser.CloseAsync().ConfigureAwait(false);
Console.WriteLine("Browser closed");Full example
Unexpected error with integration github-files: Integration is not installed on this space
How It Works
Initialize the Client: Create an instance of
BrowserServiceusing your API tokeRequest a Browser Instance: Call
svc.Open()to request a new browser session from the CloudBrowser.AI service.Connect with PuppeteerSharp: Use the WebSocket address provided in the response to connect to the browser instance using
Puppeteer.ConnectAsync().Perform Browser Actions: Interact with the browser via PuppeteerSharp, such as navigating to a webpage or automating tasks.
Close the Browser: Either use PuppeteerSharp's
browser.CloseAsync()or CloudBrowser.AI'ssvc.Close()API to terminate the session.
Open (with parameters)
The following example demonstrates how to request a browser instance with advanced configuration, including custom browser settings, proxy, and a timeout for keeping the browser open.
Label: Set a custom label for the browser instance.
Browser: Choose the browser type (e.g., Chromium or Chrome).
KeepOpen: Set the timeout in seconds for how long the browser should remain open without a Puppeteer connection.
Proxy: Configure proxy settings (host, port, username, password).
Full example
Last updated