方法一
一、准备工作
- 创建一个.Net6.0控制台项目
- 打开nuget包管理器,安装包:Microsoft.Playwright
- 编译程序,项目编译目录可以看到playwright.ps1文件
二、常用指令
//打开powershell终端执行以下命令(./playwright.ps1表示当前文件夹下)
安装Playwright支持的浏览器:powershell ./playwright.ps1 install
启动Playwright录制:powershell ./playwright.ps1 codegen www.baidu.com
方法二
一、准备工作
- 创建一个.Net6.0控制台项目
- 打开nuget包管理器,安装包:Microsoft.Playwright
二、常用指令
//打开powershell终端执行以下命令
安装Playwright工具:dotnet tool install --global Microsoft.Playwright.CLI
安装Playwright支持的浏览器:playwright install
启动Playwright录制:playwright codegen www.baidu.com
使用示例
using Microsoft.Playwright;
using System.Threading.Tasks;
namespace ConsoleApp1
{
internal class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Hello, World!");
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync(
new BrowserTypeLaunchOptions {Channel="msedge", Headless=false });
var page = await browser.NewPageAsync();
await page.GotoAsync("https://www.baidu.com");
await page.ScreenshotAsync(new PageScreenshotOptions { Path = "screenshot.png" });
}
}
}