在C#中使用Playwright模擬用戶行為包括以下幾個步驟:
安裝Playwright NuGet包:在Visual Studio中打開NuGet包管理器,搜索并安裝Playwright庫。
創建Playwright實例:在代碼中創建一個Playwright實例,用于執行瀏覽器操作。
using Microsoft.Playwright;
var playwright = await Playwright.CreateAsync();
var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false });
var page = await browser.NewPageAsync();
await page.GotoAsync("https://www.example.com");
await page.ClickAsync("button");
await page.FillAsync("input[name='username']", "exampleuser");
await page.ScreenshotAsync(new PageScreenshotOptions { Path = "screenshot.png" });
var title = await page.TitleAsync();
Console.WriteLine($"Page title: {title}");
await browser.CloseAsync();
通過上述步驟,就可以在C#中使用Playwright模擬用戶行為,執行自動化測試或者爬取網頁數據等任務。