Puppeteer Sharp Part 1
Puppeteer Sharp is a .NET port of the official Node.JS Puppeteer API.
Step 1: Create .net console or Wpf project
Project Sdk="Microsoft.NET.Sdk.WindowsDesktop"> <PropertyGroup> <OutputType>WinExe</OutputType> <TargetFramework>netcoreapp3.1</TargetFramework> <UseWPF>true</UseWPF> </PropertyGroup> </Project>
Step 2: Add PuppeteerSharp package reference
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop"> <PropertyGroup> <OutputType>WinExe</OutputType> <TargetFramework>netcoreapp3.1</TargetFramework> <UseWPF>true</UseWPF> </PropertyGroup> <ItemGroup> <PackageReference Include="PuppeteerSharp" Version="2.0.3" /> </ItemGroup> </Project>
Step 3: Install Chromium
https://chromium.woolyss.com/download/en/
Step 4: Launch options with Chromium Path
var launchOptions = new LaunchOptions() { Headless=false, ExecutablePath= @"C:\Chromium\chrome.exe" }; //Other options is if you want to auto download chromium await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
Step 5: Close any existing browser
Do this step if you want close any existing browser page
if (browser != null) { await browser.CloseAsync(); }
Step 6: Launch brower with launch option
browser = await Puppeteer.LaunchAsync(launchOptions);
Step 7: Open new page and load website
var page = await browser.NewPageAsync(); await page.GoToAsync("https://www.websocket.org/echo.html");
Step 8: Click connect button
await page.ClickAsync(".echo-button");
Step 9: Clear any existing text in sendMessage textbox using js script
//clear textbox using Puppeteer and js script await page.EvaluateExpressionAsync("document.querySelector('#sendMessage').value =''");
Step 10: Type and text and click send button
await page.TypeAsync("#sendMessage", ".net") ; await page.ClickAsync("#send"); await page.EvaluateExpressionAsync("document.querySelector('#sendMessage').value =''"); await page.TypeAsync("#sendMessage", "olasdsds"); await page.ClickAsync("#send");