【问题标题】:Chrome driver is not working on azure web appsChrome 驱动程序不适用于 azure Web 应用程序
【发布时间】:2019-07-19 05:15:18
【问题描述】:

我使用过 .NetCore2 应用程序并尝试截取给定 URL 的屏幕截图。它在本地运行完美,但部署到 Azure 后创建 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:\Projects\TFT\Bitbucket-Linkury\Website\Tools\ScreenshotAPI\DotNetCore\SceenshotApp\SceenshotApp\Service\Screenshot.cs:line 21
↵   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"

在我的代码下面

 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;
    }

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

【问题讨论】:

  • 由于 azure web app 沙箱,azure web app 不支持 selenium。
  • @Ivan Yang : 谢谢回复,还有其他选择吗?
  • 这不是一个好的解决方案,但如果部署到 azure vm 上它可以工作。
  • 我的路径在 Nuget 包 Selenium.Chrome.WebDriver 下
  • @Dimitar :我正在使用 var driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), options);

标签: c# azure .net-core selenium-chromedriver azure-web-app-service


【解决方案1】:

正如@IvanYang 所说,Windows Azure App Service 不支持Selenium,如下图来自Azure Web App sandbox

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

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

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

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

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    • 1970-01-01
    • 2012-09-10
    • 1970-01-01
    • 2022-01-28
    • 1970-01-01
    • 2016-03-24
    相关资源
    最近更新 更多