Chrome 驱动程序无法在 Azure Web 应用程序上运行

Nay*_*ori 3 c# azure selenium-chromedriver .net-core azure-web-app-service

我使用过 .NetCore2 App 并尝试截取给定 URL 的屏幕截图。它在本地运行完美,但部署到 Azure 后在创建 Webdriver 时出现问题。

\n\n
 at OpenQA.Selenium.DriverService..ctor(String servicePath, Int32 port, String driverServiceExecutableName, Uri driverServiceDownloadUrl)\n\xe2\x86\xb5   at OpenQA.Selenium.Chrome.ChromeDriverService..ctor(String executablePath, String executableFileName, Int32 port)\n\xe2\x86\xb5   at OpenQA.Selenium.Chrome.ChromeDriver..ctor(String chromeDriverDirectory, ChromeOptions options)\n\xe2\x86\xb5   at SceenshotApp.Service.Screenshot.TakeScreenshot(String url, Int32 width, Int32 height, Int32 delay) in D:\\Projects\\TFT\\Bitbucket-Linkury\\Website\\Tools\\ScreenshotAPI\\DotNetCore\\SceenshotApp\\SceenshotApp\\Service\\Screenshot.cs:line 21\n\xe2\x86\xb5   at SceenshotApp.Controllers.HomeController.TakeScreenshot(String url, Int32 width, Int32 height, Int32 scale, Int32 delay) in D:\\Projects\\TFT\\Bitbucket-Linkury\\Website\\Tools\\ScreenshotAPI\\DotNetCore\\SceenshotApp\\SceenshotApp\\Controllers\\HomeController.cs:line 51"\n
Run Code Online (Sandbox Code Playgroud)\n\n

在我的代码下面

\n\n
 public static string GetScreenshot(string url)\n    {\n        ChromeOptions options = new ChromeOptions();\n        var driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), options);\n        driver.Manage().Window.Size = new System.Drawing.Size(1000, 768);\n        driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromMilliseconds(100);\n        driver.Navigate().GoToUrl(url);\n        driver.Close();\n        driver.Quit();\n        return path;\n    }\n
Run Code Online (Sandbox Code Playgroud)\n\n

如何在 Azure 上使用 Chrome 驱动程序?

\n

Pet*_*Pan 6

正如 @IvanYang 所说,SeleniumWindows 的 Azure 应用服务不支持,如下图所示Azure Web App sandbox

在此输入图像描述

原因是Win32k.sys (User32/GDI32) Restrictions

在此输入图像描述

但是,您可以尝试将 .net core 应用程序部署到基于 docker 映像的 Linux 上的 Azure 应用服务。

因此,您可以按照快速入门教程Create an ASP.NET Core app in App Service on Linux将当前的应用程序迁移到 Linux。并且由于Selenium需要headless chrome,所以必须先在docker镜像中安装chromiumchrome或他们的headless发行版和webdriver或写入Dockerfile中,请参考官方文档Tutorial: Build a custom image and run in App Service from a private registry了解。

作为参考,有很多对您有帮助的博客,您可以通过 Google/Bing 搜索,例如Selenium in Docker with DotNetCore Chrome in Linux and Headless Mode

希望能帮助到你。