06-04-2025, 04:29 AM
Tested, Playwright can be used.
Setup (if you don't have Playwright and Playwrighter): see Cookbook recipe "Web browser automation with Playwright".
Script to run your program with Playwright connections enabled:
Script to show the Playwright recorder for your app:
The Playwright recorder does not work well in "connect" mode. Recording works, but it leaves some artifacts in the UI, and next time recording does not work. After recording, restart your app.
Example script that finds and clicks a link in your app:
Setup (if you don't have Playwright and Playwrighter): see Cookbook recipe "Web browser automation with Playwright".
Script to run your program with Playwright connections enabled:
Environment.SetEnvironmentVariable("WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS", "--remote-debugging-port=9225");
run.it(@"C:\path to your program.exe");Script to show the Playwright recorder for your app:
// script "Test Playwright WebView2 - record.cs"
/*/ c Playwrighter.cs; /*/
using var play = Playwrighter.Connect(out var page, "*", new(9225), debug: true);
var w1 = wnd.find(10, "Playwright Inspector*", "Chrome_WidgetWin_1");
w1.WaitForClosed(0);The Playwright recorder does not work well in "connect" mode. Recording works, but it leaves some artifacts in the UI, and next time recording does not work. After recording, restart your app.
Example script that finds and clicks a link in your app:
// script "Test Playwright WebView2.cs"
/*/ c Playwrighter.cs; /*/
using Microsoft.Playwright;
using var play = Playwrighter.Connect(out var page, "*", new(9225));
print.it(page.Url);
//this code was recorded with the Playwright recorder (copied from its window)
await page.GetByRole(AriaRole.Link, new() { Name = "Forum" }).ClickAsync();