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
BrowserService
using 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).
using BrowserService svc = new("YOUR CLOUDBROWSER.AI TOKEN");
// Request a Cloud Browser instance with advanced settings
var rp = await svc.Open(new() {
Label = "MyCustomBrowser", // Custom label for the browser instance
// Chromium is supported but we recommend Chrome for best stealth
Browser = CloudBrowserAiSharp.Browser.Types.SupportedBrowser.Chromium,
KeepOpen = 10 * 60, // This browser will close after 10 minutes without Puppeteer connection
Proxy = new() {
Host = "IP.0.0.0.1",
Port = "3000",
Password = "password",
Username = "username",
}
}).ConfigureAwait(false);
Full example
Last updated