Chrome 驱动程序不适用于 azure Web 应用程序

Chrome driver is not working on azure web apps(Chrome 驱动程序不适用于 azure Web 应用程序)

本文介绍了Chrome 驱动程序不适用于 azure Web 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 在 OpenQA.Selenium.DriverService..ctor(String servicePath, Int32 port, String driverServiceExecutableName, Uri driverServiceDownloadUrl)↵ 在 OpenQA.Selenium.Chrome.ChromeDriverService..ctor(字符串可执行路径,字符串可执行文件名,Int32 端口)↵ 在 OpenQA.Selenium.Chrome.ChromeDriver..ctor(字符串 chromeDriverDirectory,ChromeOptions 选项)↵ 在 D:ProjectsTFTBitbucket-LinkuryWebsiteToolsScreenshotAPIDotNetCoreSceenshotAppSceenshotAppServiceScreenshot 中的 SceenshotApp.Service.Screenshot.TakeScreenshot(字符串 url,Int32 宽度,Int32 高度,Int32 延迟).CS:第 21 行↵ 在 D:ProjectsTFTBitbucket-LinkuryWebsiteToolsScreenshotAPIDotNetCoreSceenshotAppSceenshotAppControllers 中的 SceenshotApp.Controllers.HomeController.TakeScreenshot(String url, Int32 width, Int32 height, Int32 scale, Int32 delay)HomeController.cs:第 51 行"

在我的代码下面

 public static string GetScreenshot(string url){ChromeOptions 选项 = 新 ChromeOptions();var driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), options);driver.Manage().Window.Size = new System.Drawing.Size(1000, 768);driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromMilliseconds(100);driver.Navigate().GoToUrl(url);司机.关闭();driver.Quit();返回路径;}

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

解决方案

正如@IvanYang 所说,Selenium 在 Azure App Service for Windows 上是不支持的,如下图来自

原因是

但是,您可以尝试将 .net 核心应用部署到 Linux 上的 Azure 应用服务,该服务基于 docker 映像.

所以你可以按照快速入门教程<代码>在 Linux 上的应用服务中创建 ASP.NET Core 应用 以迁移当前适用于 Linux 的应用.由于 Selenium 需要 headless chrome,您必须在 docker 镜像中安装 chromiumchrome 或其无头发行版和 webdriver 或写入首先Dockerfile,请参考官方文档 教程:构建自定义镜像并从私有注册表在应用服务中运行了解它.

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

希望对你有帮助.

I have used .NetCore2 App and try to takes the screenshot of given URL. It works perfect on local but After deploy to Azure have problems on create Webdriver.

 at OpenQA.Selenium.DriverService..ctor(String servicePath, Int32 port, String driverServiceExecutableName, Uri driverServiceDownloadUrl)
↵   at OpenQA.Selenium.Chrome.ChromeDriverService..ctor(String executablePath, String executableFileName, Int32 port)
↵   at OpenQA.Selenium.Chrome.ChromeDriver..ctor(String chromeDriverDirectory, ChromeOptions options)
↵   at SceenshotApp.Service.Screenshot.TakeScreenshot(String url, Int32 width, Int32 height, Int32 delay) in D:ProjectsTFTBitbucket-LinkuryWebsiteToolsScreenshotAPIDotNetCoreSceenshotAppSceenshotAppServiceScreenshot.cs:line 21
↵   at SceenshotApp.Controllers.HomeController.TakeScreenshot(String url, Int32 width, Int32 height, Int32 scale, Int32 delay) in D:ProjectsTFTBitbucket-LinkuryWebsiteToolsScreenshotAPIDotNetCoreSceenshotAppSceenshotAppControllersHomeController.cs:line 51"

below my code

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

How can I use Chrome driver on Azure?

解决方案

As @IvanYang said, Selenium is not supported on Azure App Service for Windows, as the figure below from Azure Web App sandbox.

The reason is Win32k.sys (User32/GDI32) Restrictions

However, you can try to deploy your .net core app to Azure App Service on Linux, which be based on docker image.

So you can follow the quick start tutorial Create an ASP.NET Core app in App Service on Linux to migrate your current app for Linux. And due to Selenium requires headless chrome, you must have to install chromium or chrome or their headless distributions and webdriver in the docker image or write in the Dockerfile first, please refer to the offical document Tutorial: Build a custom image and run in App Service from a private registry to know it.

As reference, there are many blogs which helps for you and you can search via Google/Bing, such as Selenium in Docker with DotNetCore Chrome in Linux and Headless Mode.

Hope it helps.

这篇关于Chrome 驱动程序不适用于 azure Web 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Chrome 驱动程序不适用于 azure Web 应用程序

基础教程推荐