Chrome 驱动程序不适用于 azure 网络应用程序 [英] Chrome driver is not working on azure web apps
问题描述
我使用了 .NetCore2 App 并尝试截取给定 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)↵ 在 OpenQA.Selenium.Chrome.ChromeDriver..ctor(字符串 chromeDriverDirectory,ChromeOptions 选项)↵ 位于 D:ProjectsTFTBitbucket-LinkuryWebsiteToolsScreenshotAPIDotNetCoreSceenshotAppSceenshotAppServiceScreenshot 中的 SceenshotApp.Service.Screenshot.TakeScreenshot(String url, Int32 width, Int32 height, Int32 delay).cs:第21行↵ 在 D:ProjectsTFTBitbucket-LinkuryWebsiteToolsScreenshotAPIDotNetCoreSceenshotAppSceenshotAppControllers 中的 SceenshotApp.Controllers.HomeController.TakeScreenshot(String url, Int32 width, Int32 height, Int32 scale, Int32 delay)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);驱动程序关闭();驱动程序退出();返回路径;}
如何在 Azure 上使用 Chrome 驱动程序?
正如@IvanYang 所说,Selenium
在 Windows 的 Azure 应用服务上不支持,如下图来自
原因是
但是,您可以尝试将 .net 核心应用程序部署到 Linux 上的 Azure 应用服务,该服务基于 docker 映像.
因此您可以按照快速入门教程在 Linux 上的应用服务中创建 ASP.NET Core 应用
以将您当前的应用迁移到 Linux.并且由于 Selenium
需要无头 chrome,您必须在 docker 镜像中安装 chromium
或 chrome
或它们的无头发行版和 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 网络应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!