Playwright在Ubuntu上的安装问题,出现浏览器装不上时的解决方案

在老外博客上看到的
实际上载Playwright里有安装的代码,命令行也是调用其中的而已
不过不知道为什么始终有问题,在代码前用代码执行安装就能用了原博客
下面的代码直接运行即可

Console.WriteLine("Installing browsers");

// The following line installs the default browsers. If you only need a subset of browsers,
// you can specify the list of browsers you want to install among: chromium, chrome,
// chrome-beta, msedge, msedge-beta, msedge-dev, firefox, and webkit.
// var exitCode = Microsoft.Playwright.Program.Main(new[] { "install", "webkit", "chrome" });
// If you need to install dependencies, you can add "--with-deps"
var exitCode = Microsoft.Playwright.Program.Main(new[] { "install" });
if (exitCode != 0)
{
    Console.WriteLine("Failed to install browsers");
    Environment.Exit(exitCode);
}

Console.WriteLine("Browsers installed");

// Using playwright to launch a browser
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
{
  Headless  = false
});
await using var browserContext = await browser.NewContextAsync();
var page = await browserContext.NewPageAsync();
await page.GotoAsync("https://www.baidu.com");

Console.ReadLine();

还会出现这样的问题
Microsoft.Playwright assembly was found, but is missing required assets. Please ensure to build your project before running Playwright tool.
对于服务器, 最好安装sdk 因为自动安装要用到cli工具

dotnet tool install --global Microsoft.Playwright.CLI
安装完后重启,并且在编译目录下找到
Microsoft.Playwright.dll 放到根目录下

对于提示的权限问题, 对应文件或者文件夹打开权限即可

成功后第一次使用

完成后还有东西需要安装

这里贴出来粘贴安装就好

apt-get install libatk1.0-0\
         libatk-bridge2.0-0\
         libcups2\
         libxkbcommon0\
         libatspi2.0-0\
         libxdamage1\
         libgbm1\
         libpango-1.0-0\
         libcairo2

这步作文就大功告成了

posted @ 2023-02-07 09:59  Z_DK  阅读(445)  评论(0编辑  收藏  举报