我使用 Selenium 创建了一个 .Net 应用程序。本地一切工作正常,但是当在 Docker 容器中使用我的应用程序时,我总是收到以下异常
System.InvalidOperationException: session not created: Chrome failed to start: exited normally.
(chrome not reachable)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.) (SessionNotCreated)
at OpenQA.Selenium.WebDriver.UnpackAndThrowOnError(Response errorResponse, String commandToExecute)
at OpenQA.Selenium.WebDriver.ExecuteAsync(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.WebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.WebDriver.StartSession(ICapabilities capabilities)
at OpenQA.Selenium.WebDriver..ctor(ICommandExecutor executor, ICapabilities capabilities)
at OpenQA.Selenium.Chromium.ChromiumDriver..ctor(ChromiumDriverService service, ChromiumOptions options, TimeSpan commandTimeout)
at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout)
at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeOptions options)
在 .Net 应用程序中创建 Chrome 驱动程序时,我使用以下参数
var chromeOptions = new ChromeOptions();
if (OperatingSystem.IsLinux())
{
chromeOptions.BinaryLocation = "/usr/bin/google-chrome"; // Set the correct path to the Chrome binary
}
chromeOptions.AddArgument("--disable-dev-shm-usage");
chromeOptions.AddArgument("--no-sandbox");
chromeOptions.AddArgument("--disable-search-engine-choice-screen");
_webDriver = new ChromeDriver(chromeOptions);
我正在使用 Selenium Chrome 图像,而它包含我的 .Net 应用程序,该应用程序作为独立发布。
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM selenium/standalone-chrome:latest AS base
USER app
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["MyApp.csproj", "MyApp/"]
RUN dotnet restore "./MyApp.csproj"
COPY . .
WORKDIR "/src/MyApp"
RUN dotnet build "./MyApp.csproj" -c $BUILD_CONFIGURATION -o /app/build
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./MyApp.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=true --self-contained true -r linux-x64
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["./MyApp"]
EXPOSE 5000
我在尝试在本地运行容器的云上都遇到了这个问题。
通过添加以下内容,问题得到解决
chromeOptions.AddArgument("--remote-debugging-port=9222");