Automate Browser using Puppeteer Sharp in C#
In this article, I will talk about Puppeteer Sharp.
Puppeteer is a Node library which provides a high-level API to control Chrome or Chromium over the DevTools Protocol. Puppeteer runs headless by default, but can be configured to run full (non-headless) Chrome or Chromium.
Before going further, I assume, you have an idea about HtmlAgilityPack.
HAP (HtmlAgilityPack) is an HTML parser written in C# to read/write DOM and supports plain XPATH or XSLT.
Don’t worry, I will talk about HtmlAgilityPack in a separate article.
So, Let’s get started.
The most common use cases for it is logging into different websites. In this article, we will walk through the process of logging into LinkedIn using PuppeteerSharp.
To get started with PuppeteerSharp, you first need to install the package through NuGet.
dotnet add package PuppeteerSharp
To log into LinkedIn, we will first navigate to the login page. Before navigation to login page. we need to download ChromiumBrowser
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultChromiumRevision);
In order to open the browser we will use the Puppeteer.LaunchAsync()
method to launch a new browser instance and interact with web pages.
Puppeteer.LaunchAsync
method takes an optional LaunchOptions
object as a parameter, which can be used to configure the browser instance.
Headless
: A boolean value indicating whether to launch the browser in headless mode (true) or with a visible UI (false)ExecutablePath
: A string representing the path to the Chromium executableSlowMo
: A value in milliseconds that slows down Puppeteer operations to make them easier to observe.Timeout
: A value in milliseconds that sets the maximum time to wait for the browser instance to start.
For the time being we will use Headless
only.
var browser = await Puppeteer.LaunchAsync(new LaunchOptions() { Headless = false })
Next, We will open a tab first then navigate to LinkedIn login page through following command:
// Open a new tab
var page = await browser.NewPageAsync();
// Navigate to the LinkedIn login page
await page.GoToAsync("https://www.linkedin.com/login");
When login page is loaded successfully. then we fill the email
and password
field by Page
class object page
. Let’s see how to it will do.
// Fill in the email field
await page.TypeAsync("input[name=session_key]", "your_email");
// Fill in the password field
await page.TypeAsync("input[name=session_password]", "your_password");
After filling the both fields, we need to click the Login
button.
// Click the login button
await page.ClickAsync("button.from__button--floating");
Following line, wait for the login to complete.
await page.WaitForNavigationAsync();
When login is finished we will navigate to notification
page (LinkedIn notification page).
await page.GoToAsync("https://www.linkedin.com/notifications");
Here is the full code of our article
async Task AutomateLinkedInHomePage()
{
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultChromiumRevision);
var browser = await Puppeteer.LaunchAsync(new LaunchOptions() { Headless = false });
// Open a new tab
var page = await browser.NewPageAsync();
// Navigate to the LinkedIn login page
await page.GoToAsync("https://www.linkedin.com/login");
// Fill in the email field
await page.TypeAsync("input[name=session_key]", "your_email");
// Fill in the password field
await page.TypeAsync("input[name=session_password]", "your_password");
// Click the login button
await page.ClickAsync("button.from__button--floating");
await page.WaitForNavigationAsync();
await page.GoToAsync("https://www.linkedin.com/notifications");
}
This is done for now. Hopefully, you enjoyed it. Have a nice day !!!