1.簡單使用實例 1.1 添加log4net.dll的引用。 在NuGet程式包中搜索log4net並添加,此次我所用版本為2.0.17。如下圖: 1.2 添加配置文件 右鍵項目,添加新建項,搜索選擇應用程式配置文件,命名為log4net.config,步驟如下圖: 1.2.1 log4net.co ...
安裝全局工具:
# 安裝全局工具 dotnet tool install --global Microsoft.Playwright.CLI # 創建項目 dotnet new console -n Console1 cd Console1 # 安裝依賴 dotnet add package Microsoft.Playwright # 用Playwright 工具安裝所需的瀏覽器 C:\Users\Administrator\AppData\Local\ms-playwright playwright install # 運行項目 dotnet run
在新的環境中部署:
# 安裝全局工具 dotnet tool install --global Microsoft.Playwright.CLI # 用Playwright 工具安裝瀏覽器(要在項目部署的目錄) dotnet Microsoft.Playwright.dll -- install
基本使用:
using var playwright = await Playwright.CreateAsync(); await using var browser = await playwright.Chromium.LaunchAsync(); var page = await browser.NewPageAsync(); await page.GotoAsync("https://www.baidu.com"); var title = await page.InnerTextAsync("title");
Playwright Trace Viewer工具來追蹤測試執行:
using var playwright = await Playwright.CreateAsync(); await using var browser = await playwright.Chromium.LaunchAsync(new() { Headless = true }); var context = await browser.NewContextAsync(); #region 開始追蹤記錄 await context.Tracing.StartAsync(new() { Title = $"qwe123", Screenshots = true, Snapshots = true, Sources = true }); #endregion var page = await context.NewPageAsync();//await browser.NewPageAsync(); await page.GotoAsync("https://www.baidu.com"); //將上面鏈接打開後的page頁面截圖 保存成 screenshot.png await page.ScreenshotAsync(new PageScreenshotOptions { Path = Path.Combine( Environment.CurrentDirectory, "screenshot", $"{DateTime.Now.ToString("yyyyMMddHHmmss")}.png" ) }); var title = await page.InnerTextAsync("title"); #region 結束追蹤記錄 await context.Tracing.StopAsync(new() { Path = Path.Combine( Environment.CurrentDirectory, "playwright-traces", $"{DateTime.Now.ToString("yyyyMMddHHmmss")}.zip" ) }); #endregion
Playwright Trace Viewer工具記錄多個追蹤(trace chunk):
using var playwright = await Playwright.CreateAsync(); await using var browser = await playwright.Chromium.LaunchAsync(new() { Headless = true }); var context = await browser.NewContextAsync(); #region 開始追蹤記錄 await context.Tracing.StartAsync(new() { Title = $"qwe123", Screenshots = true, Snapshots = true, Sources = true }); #endregion #region 開始追蹤記錄 await context.Tracing.StartChunkAsync(new() { Title = $"qwe123" }); #endregion var page = await context.NewPageAsync();//await browser.NewPageAsync(); await page.GotoAsync("https://www.baidu.com"); //將上面鏈接打開後的page頁面截圖 保存成 screenshot.png await page.ScreenshotAsync(new PageScreenshotOptions { Path = Path.Combine( Environment.CurrentDirectory, "screenshot", $"{DateTime.Now.ToString("yyyyMMddHHmmss")}.png" ) }); var title = await page.InnerTextAsync("title"); #region 結束追蹤記錄 await context.Tracing.StopChunkAsync(new() { Path = Path.Combine( Environment.CurrentDirectory, "playwright-traces", $"{DateTime.Now.ToString("yyyyMMddHHmmss")}1.zip" ) }); #endregion #region 開始追蹤記錄 await context.Tracing.StartChunkAsync(new() { Title = $"qwe1234" }); #endregion await page.GotoAsync("https://www.baidu.com/"); // 等待頁面載入完成 await page.WaitForLoadStateAsync(LoadState.NetworkIdle); await page.GetByRole(AriaRole.Link, new() { Name = "登錄" }).ClickAsync(); await page.GetByPlaceholder("手機號/用戶名/郵箱").ClickAsync(); await page.GetByPlaceholder("手機號/用戶名/郵箱").FillAsync("123456"); await page.GetByPlaceholder("密碼").ClickAsync(); await page.GetByPlaceholder("密碼").FillAsync("qwe123"); await page.GetByRole(AriaRole.Checkbox, new() { Name = "閱讀並接受" }).CheckAsync(); await page.GetByRole(AriaRole.Button, new() { Name = "登錄" }).ClickAsync(); #region 結束追蹤記錄 await context.Tracing.StopChunkAsync(new() { Path = Path.Combine( Environment.CurrentDirectory, "playwright-traces", $"{DateTime.Now.ToString("yyyyMMddHHmmss")}2.zip" ) }); #endregion
本地打開跟蹤文件:
playwright show-trace 12344.zip
開啟Playwright錄製操作自動生成代碼腳本:
cd D:\Playwright\ConsoleApp1\ConsoleApp1
playwright codegen
Playwright發佈與部署:
部署模式選擇“獨立”,可以不依賴框架運行
另一臺機器上運行我們的程式之前,需要在這台機器上重新安裝 Playwright,但無需安裝 dotnet 框架或運行時。
運行發佈文件夾下的安裝命令:
.playwright\node\win32_x64\playwright.cmd install
常用操作:
// 創建Chromium瀏覽器實例 await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions() { Headless = false, // 關閉無頭模式(有界面) SlowMo = 50, // 放慢執行速度 Channel = "chrome", // 指定採用chrome瀏覽器類型 Devtools = true, // 啟用開發者工具 ChromiumSandbox = false, // 關閉瀏覽器沙盒 ExecutablePath = string.Empty, // 不指定瀏覽器可執行文件位置,會自動尋找 ms-playwright 下載的瀏覽器 Args = new[] { "--enable-automation=true", "--disable-blink-features=AutomationControlled" }, // 防止selenium被檢測 }); // 瀏覽器上下文 await using var context = await browser.NewContextAsync(new BrowserNewContextOptions { ViewportSize = new ViewportSize { Width = 1536, // 1920 * 0.8 Height = 864, // 1080 * 0.8 }, // 視窗大小 Locale = "zh-CN", // 指定語言(區域) TimezoneId = "Asia/Shanghai", // 指定時區 }); // 存儲當前會話 await context.StorageStateAsync(new BrowserContextStorageStateOptions { Path = "state.json" }); // 使用會話,當state.json文件不存在時會報錯 await browser.NewContextAsync(new BrowserNewContextOptions { StorageStatePath = "state.json" }); // 等待登錄按鈕出現 await page.WaitForSelectorAsync(".login"); // 點擊登錄 await page.ClickAsync(".login"); // 查找頁面元素 可匹配到 <div class="title-text-n4df3g8">美食</div> var locator = page.Locator(“div[class^='title-text-']:text-is('美食')”); // 獲取匹配到元素的個數 var count = await locator.CountAsync(); // 滑鼠懸停在元素上 await page.HoverAsync("#title"); // 執行js獲取頁面地址 var href = await page.EvaluateAsync<string>("document.location.href"); // 定位器 var element = page.Locator(".passMod_dialog-close-btn"); // 等待1秒 await Task.Delay(1000); await element.ClickAsync(); // 顯式等待 設定的超時時間內,仍然沒有通過定位器等待到目標元素,將會拋出異常 await page.GotoAsync("https://stackoverflow.com/"); var elelment = page.Locator("id=noscript-warning"); await elelment.WaitForAsync(); var text = await elelment.InnerTextAsync(new LocatorInnerTextOptions { Timeout = 10 }); // RunAndWaitForPopupAsync 方法可以幫助我們執行操作並等待彈出視窗出現 await page.GotoAsync("https://www.baidu.com/"); await page.RunAndWaitForPopupAsync(async () => { await page.ClickAsync("text=新聞"); }, new PageRunAndWaitForPopupOptions { Predicate = (page) => { return page.Url.Contains("news.baidu.com"); } }); // RunAndWaitForRequestAsync/RunAndWaitForResponseAsync兩個方法可以通過傳入URL地址或表達式來過濾等待的請求或響應,從而實現更加精細的控制。方法都是阻塞式的。也就是說,如果我們等待的情況一直未出現,後續的代碼將不會執行。
// 在某些情況下,我們只需要在情況出現時進行一些操作,即使情況不出現也不應該影響整體的運行。這時,我們可以使用監聽事件來實現。 await page.RunAndWaitForRequestAsync(async () => { await page.GotoAsync("https://stackoverflow.com/", new PageGotoOptions{ WaitUntil = WaitUntilState.Commit}); }, request => { Console.WriteLine(request.Url); //return request.Url.Contains("jquery.min.js"); return request.Url.Contains("stacks.min.js"); }); await page.RunAndWaitForResponseAsync(async () => { await page.ClickAsync("text='Login'"); }, response => response.Url.Contains("users/login") ); // 當頁面發起的請求失敗時觸發 page.RequestFailed += (sender, request) => { Console.WriteLine($"請求{request.Url}發生錯誤{request.Failure}"); }; // 當頁面發起的請求完成時觸發 page.RequestFinished += (sender, request) => { Console.WriteLine($"請求{request.Url}發生錯111111誤{request.Failure}"); }; await page.GotoAsync("https://baidu.com/"); // Close:當頁面關閉時觸發 // Console:當控制台輸出時觸發 // Crash:當頁面崩潰時觸發。例如,試圖分配太多記憶體,瀏覽器頁面可能會崩潰。 // Dialog:當 JavaScript 對話框出現時觸發。例如,alert、prompt、confirm等。 // DOMContentLoaded:當頁面的 DOMContentLoaded 事件觸發時觸發 // Download:當下載開始時觸發 // FileChooser:當文件選擇對話框出現時觸發 // FrameAttached:當 frame 連接到頁面時觸發 // FrameDetached:當 frame 與頁面分離時觸發 // FrameNavigated:當 frame 導航到一個新的 url 時觸發 // Load: 當頁面的 load 事件觸發時觸發 // PageError:當頁面內發生未捕獲異常時觸發 // Popup:當頁面彈出新的視窗時觸發 // Request:當頁面發起一個新的請求時觸發 // RequestFailed:當頁面發起的請求失敗時觸發 // RequestFinished:當頁面發起的請求完成時觸發 // Response:當頁面收到一個新的響應時觸發 // 輸入搜索關鍵字 await page.TypeAsync("input[name='q1']", "關鍵字"); // 點擊page按鈕跳轉到page1 var page1 = await page.RunAndWaitForPopupAsync(async () => { // 點擊搜索 await page.ClickAsync(".advanced-search-btn"); }); // 檢查文本框內容 var handleInput = await page1.WaitForSelectorAsync("#kw"); var text = await handleInput.GetAttributeAsync("value"); // 輸入操作 await page.Locator("#kw").FillAsync("1212121"); // 點擊操作 await page.Locator("#kw").ClickAsync(); // 單選(選中,已選中狀態下無效) await page.Locator(".check1").SetCheckedAsync(true); await page.Locator(".Volvo").CheckAsync(); // 覆選框(選中,已選中狀態下無效) await page.Locator("#checkbox [type='checkbox']:nth-child(7)").CheckAsync(); await page.Locator("#checkbox [type='checkbox']:nth-child(7)").SetCheckedAsync(true); // 下拉框操作 // 選擇下拉框中的選項 await page.SelectOptionAsync("select[name='country']", "Canada"); // 通過value值選擇 await page.Locator("[name='select']").SelectOptionAsync("opel"); // 通過可見文本選擇 await page.Locator("[name='select']").SelectOptionAsync(new SelectOptionValue { Label = "blue" }); // 多個選定項目 await page.GetByLabel("Choose multiple colors").SelectOptionAsync(new[] { "blue", "green", "red" }); // 常規單擊 await page.Locator("#mouse2").ClickAsync(); // 單擊 await page.GetByRole(AriaRole.Button).ClickAsync(); // 雙擊 await page.GetByText("Item").DblClickAsync(); // 右擊 await page.GetByText("Item").ClickAsync(new() { Button = MouseButton.Right }); // Shift + click await page.GetByText("Item").ClickAsync(new() { Modifiers = new[] { KeyboardModifier.Shift } }); // Ctrl + click or Windows and Linux // Meta + click on macOS await page.GetByText("Item").ClickAsync(new() { Modifiers = new[] { KeyboardModifier.ControlOrMeta } }); // Hover over element await page.GetByText("Item").HoverAsync(); // Click the top left corner await page.GetByText("Item").ClickAsync(new() { Position = new Position { X = 0, Y = 0 } }); //上傳一個文件 await page.Locator("#load").SetInputFilesAsync("demo.md"); // 上傳多個文件 await page.Locator("#load").SetInputFilesAsync(new[] { "file1.txt", "file12.txt" }); // 上傳文件 找到文件上傳輸入框 var fileInput = await page.QuerySelectorAsync("input[type='file']"); string filePath = "path/to/your/file.txt"; // 你要上傳的文件路徑 await fileInput.SetInputFilesAsync(filePath); // 提取頁面標題 string title = await page.TitleAsync(); title = await page.InnerTextAsync("title"); Console.WriteLine("Page title: " + title); // 提取頁面 URL string url = page.Url; Console.WriteLine("Page URL: " + url); // 提取特定元素的文本內容 var elementText = await page.EvaluateAsync<string>("document.querySelector('h1').textContent"); Console.WriteLine("Header text: " + elementText); // 提取頁面中的鏈接 var links = await page.QuerySelectorAllAsync("a"); foreach (var link in links) { var href = await link.GetAttributeAsync("href"); Console.WriteLine("Link: " + href); } // 等待表格載入完畢 await page.WaitForSelectorAsync("table"); // 獲取表格內容 var tableHtml = await page.InnerHTMLAsync("table"); // 使用正則表達式提取表格數據 var regex = new Regex(@"<tr>(.*?)</tr>"); var matches = regex.Matches(tableHtml); foreach (Match match in matches) { // 這裡可以根據表格結構和需要自行解析數據 var rowHtml = match.Groups[1].Value; Console.WriteLine("Row HTML: " + rowHtml); } // 等待頁面載入完成 await page.WaitForLoadStateAsync(LoadState.NetworkIdle); // 等待頁面跳轉 await page.WaitForNavigationAsync(); // RunAndWaitForFileChooserAsync 方法可以幫助我們在執行操作的同時等待文件選擇器的出現。 await page.GotoAsync("https://image.baidu.com/"); await page.RunAndWaitForFileChooserAsync(async () => { await page.ClickAsync("id=sttb"); await page.ClickAsync("id=uploadImg"); }, new PageRunAndWaitForFileChooserOptions { Predicate = (fileChooser) => { return !fileChooser.IsMultiple; } }); // RunAndWaitForNavigationAsync 方法可以幫助我們執行操作並等待頁面導航完成 await page.GotoAsync("https://stackoverflow.com/"); await page.RunAndWaitForNavigationAsync(async () => { await page.ClickAsync("text='Log in'"); }, new PageRunAndWaitForNavigationOptions { UrlFunc = (url) => { return url.Contains("users/login"); }, WaitUntil = WaitUntilState.Commit }); // JavaScript互操作 await page.ExposeFunctionAsync("sha256", (string input) => { return Convert.ToBase64String( SHA256.Create().ComputeHash(System.Text.Encoding.UTF8.GetBytes(input))); }); await page.SetContentAsync(@"<script> async function onClick() { document.querySelector('div').textContent = await window.sha256('PLAYWRIGHT'); } </script> <button onclick='onClick()'>Click me</button> <div></div>" ); await page.ClickAsync("button"); // Locator(定位器) // 按 id 查找 page.Locator("#su"); page.Locator("id=su"); // 按文本查找 page.Locator("text=hao123"); page.Locator("'hao123'"); page.Locator("text=/^ha\\w+23$/i"); // 按 CSS 查找 page.Locator("input[value='百度一下']"); page.Locator("input:text('百度')"); //按 XPath 查找 page.Locator("//*[@id=\"su\"]"); // BrowserContext(瀏覽器上下文) var browserContext1 = await browser.NewContextAsync(); var cookie1 = await GetCookie(browserContext1); Console.WriteLine(cookie1); var browserContext2 = await browser.NewContextAsync(); var cookie2 = await GetCookie(browserContext2); Console.WriteLine(cookie2); static async Task<string> GetCookie(IBrowserContext browserContext) { var page = await browserContext.NewPageAsync(); await page.GotoAsync("https://www.baidu.com"); var cookies = await browserContext.CookiesAsync(); return cookies.First(p => p.Name == "BAIDUID").Value; } // Frame(框架) foreach (var frame in page.Frames) Console.WriteLine($"{frame.Url.Split('/').Last()}"); Console.WriteLine(""); Console.WriteLine("顯示 Frame 嵌套樹:"); DumpFrameTree(page.MainFrame, string.Empty);