Azure Webjob - 尝试打开 ChromeDriver 时超时

Azure Webjob - Timeout when trying to open ChromeDriver

我正在尝试使用 Azure WebApps 和 Webjobs 来测试应用程序。 在本地,效果很好。但是当我构建到云时,它发生了上面列出的问题。

显然已找到文件,但它一直在等待直到超时。

有人知道怎么解决吗? 我正在使用 C# Selenium WebDriver。

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Diagnostics;
using System.IO;

namespace Ololu_WebJob
{
    class Program
    {
        static void Main()
        {
            IWebDriver driver = new ChromeDriver();
            driver.Url = "my_url";

            Console.WriteLine(driver.Title);

            driver.Quit();
        }
    }
}

/*
    LOG:
    [02/10/2019 12:27:51 > 7f9c27: SYS INFO] Status changed to Initializing
    [02/10/2019 12:27:51 > 7f9c27: SYS INFO] Job directory change detected: Job file 'Ololu_WebJob.exe' timestamp differs between source and working directories.
    [02/10/2019 12:27:53 > 7f9c27: SYS INFO] Run script 'chromedriver.exe' with script host - 'WindowsScriptHost'
    [02/10/2019 12:27:53 > 7f9c27: SYS INFO] Status changed to Running
    [02/10/2019 12:27:53 > 7f9c27: INFO] Starting ChromeDriver 2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1) on port 9515
    [02/10/2019 12:27:53 > 7f9c27: INFO] Only local connections are allowed.
    [02/10/2019 12:27:53 > 7f9c27: INFO] Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
    [02/10/2019 12:31:55 > 7f9c27: ERR ] Command 'cmd /c ""chromedriv ...' was aborted due to no output nor CPU activity for 121 seconds. You can increase the SCM_COMMAND_IDLE_TIMEOUT app setting (or WEBJOBS_IDLE_TIMEOUT if this is a WebJob) if needed.
    cmd /c ""chromedriver.exe""
    [02/10/2019 12:31:55 > 7f9c27: SYS INFO] Status changed to Failed
    [02/10/2019 12:31:55 > 7f9c27: SYS ERR ] System.AggregateException: One or more errors occurred. ---> Kudu.Core.Infrastructure.CommandLineException: Command 'cmd /c ""chromedriv ...' was aborted due to no output nor CPU activity for 121 seconds. You can increase the SCM_COMMAND_IDLE_TIMEOUT app setting (or WEBJOBS_IDLE_TIMEOUT if this is a WebJob) if needed.
    cmd /c ""chromedriver.exe""
       at Kudu.Core.Infrastructure.IdleManager.WaitForExit(IProcess process) in C:\Kudu Files\Private\src\master\Kudu.Core\Infrastructure\IdleManager.cs:line 45
       at Kudu.Core.Infrastructure.ProcessExtensions.<Start>d__12.MoveNext() in C:\Kudu Files\Private\src\master\Kudu.Core\Infrastructure\ProcessExtensions.cs:line 212
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Kudu.Core.Infrastructure.Executable.<ExecuteAsync>d__31.MoveNext() in C:\Kudu Files\Private\src\master\Kudu.Core\Infrastructure\Executable.cs:line 255
       --- End of inner exception stack trace ---
       at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
       at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
       at System.Threading.Tasks.Task`1.get_Result()
       at Kudu.Core.Infrastructure.Executable.ExecuteInternal(ITracer tracer, Func`2 onWriteOutput, Func`2 onWriteError, Encoding encoding, String arguments, Object[] args) in C:\Kudu Files\Private\src\master\Kudu.Core\Infrastructure\Executable.cs:line 216
       at Kudu.Core.Infrastructure.Executable.ExecuteReturnExitCode(ITracer tracer, Action`1 onWriteOutput, Action`1 onWriteError, String arguments, Object[] args) in C:\Kudu Files\Private\src\master\Kudu.Core\Infrastructure\Executable.cs:line 165
       at Kudu.Core.Jobs.BaseJobRunner.RunJobInstance(JobBase job, IJobLogger logger, String runId, String trigger, ITracer tracer, Int32 port) in C:\Kudu Files\Private\src\master\Kudu.Core\Jobs\BaseJobRunner.cs:line 272
    ---> (Inner Exception #0) ExitCode: -1, Output: Command 'cmd /c ""chromedriv ...' was aborted due to no output nor CPU activity for 121 seconds. You can increase the SCM_COMMAND_IDLE_TIMEOUT app setting (or WEBJOBS_IDLE_TIMEOUT if this is a WebJob) if needed., Error: Command 'cmd /c ""chromedriv ...' was aborted due to no output nor CPU activity for 121 seconds. You can increase the SCM_COMMAND_IDLE_TIMEOUT app setting (or WEBJOBS_IDLE_TIMEOUT if this is a WebJob) if needed., Kudu.Core.Infrastructure.CommandLineException: Command 'cmd /c ""chromedriv ...' was aborted due to no output nor CPU activity for 121 seconds. You can increase the SCM_COMMAND_IDLE_TIMEOUT app setting (or WEBJOBS_IDLE_TIMEOUT if this is a WebJob) if needed.
    cmd /c ""chromedriver.exe""
       at Kudu.Core.Infrastructure.IdleManager.WaitForExit(IProcess process) in C:\Kudu Files\Private\src\master\Kudu.Core\Infrastructure\IdleManager.cs:line 45
       at Kudu.Core.Infrastructure.ProcessExtensions.<Start>d__12.MoveNext() in C:\Kudu Files\Private\src\master\Kudu.Core\Infrastructure\ProcessExtensions.cs:line 212
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Kudu.Core.Infrastructure.Executable.<ExecuteAsync>d__31.MoveNext() in C:\Kudu Files\Private\src\master\Kudu.Core\Infrastructure\Executable.cs:line 255<---

*/

根据我的经验,如果没有在 Azure WebApp 上安装 Chrome 或 Chromium 实例,您不能直接使用 selenium 和 chromedriver,如下面 ChromeDriver Setup 的要求.

  • Ensure Chromium/Google Chrome is installed in a recognized location

ChromeDriver expects you to have Chrome installed in the default location for your platform. You can also force ChromeDriver to use a custom location by setting a special capability.

但是,由于 Azure WebApp 沙箱的一些限制,其中包括 Win32k.sys (User32/GDI32) Restrictions and Local Address Requests,如果没有通过 chromedriver 的 GDI 支持,您无法启动 Chrome 或 Chromium,即使您上传了 Chrome 或 Chromium 进入 Azure WebApp 并配置环境变量。

虽然你的错误信息没有指出你的C#代码问题的真正原因,但我在Azure WebApp的Python中得到了如下错误信息。

因此,解决方法是在 Azure VM 上直接使用 selenium 和 chromedriver。或者考虑到您的场景,您可以在 Azure VM 的默认端口 9515 上启动 chromedriver 并配置 VM NSG 角色以允许端口 9515 的入站,然后使用 class OpenQA.Selenium.Remote.RemoteWebDriver 连接 Azure WebJob 中的远程 webdriver。

这是使用来自 C# (CSharp) OpenQA.Selenium.Remote RemoteWebDriver.FindElement Examples

RemoteWebDriver 的示例代码
IWebDriver webDriver = new RemoteWebDriver(new Uri("http://<vm host ip>:9515"), DesiredCapabilities.Chrome());