如何在Docker容器中运行 selenium 铬合金驱动程序?

6tqwzwtp  于 2022-11-22  发布在  Docker
关注(0)|答案(4)|浏览(145)

tl; d天

如何安装所有组件以在Docker容器中运行Selenium?

问题

我从这张图片开始:

FROM microsoft/aspnetcore-build:2 AS builder
WORKDIR /source

COPY . .
RUN dotnet restore
RUN dotnet build
ENTRYPOINT ["dotnet", "run"]

我如何才能使它,使我可以启动和使用无头Chrome驱动程序与此:

ChromeOptions options = new ChromeOptions();
options.AddArgument("--headless");
options.AddArgument("--disable-gpu");
var driverPath = Path.GetFullPath(Path.Combine(environment.ContentRootPath, "bin/Debug/netcoreapp2.0"));
return new ChromeDriver(driverPath, options, TimeSpan.FromSeconds(60));

在码头集装箱里吗

我尝试过什么#

安装Chrome驱动程序

chromedriver通过Selenium.WebDriver.ChromeDriver NuGet软件包分发。
安装镀铬件
在我的Mac OS X上安装了谷歌Chrome,当前的设置工作得很好。
我试着加上这几行:

RUN apt-get update && apt-get -y install libglib2.0-dev libxi6 libnss3-dev
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
RUN apt-get update && apt-get -y install google-chrome-stable

以上安装此版本的Chrome:

google-chrome-stable:
  Installed: 64.0.3282.119-1
  Candidate: 64.0.3282.119-1
  Version table:
 *** 64.0.3282.119-1 500
        500 http://dl.google.com/linux/chrome/deb stable/main amd64 Packages
        100 /var/lib/dpkg/status

该版本与Chrome驱动程序的版本兼容。
这是由于试图解决在Docker容器中运行Selenium时出现的每个错误。
如果运行此设置,则会得到:
正在端口57889上启动ChromeDriver 2.35.528139(47 ead 77 cb 35 ad 2a 9a 83248 b292151462 a66 cd 881)只允许本地连接。发送请求时出错。无法连接到
当运行容器时。

在容器中调试

如果我手动进入容器并尝试手动运行chrome驱动程序,我会得到:
在端口9515上启动ChromeDriver 2.35.528139(47 ead 77 cb 35 ad 2a 9a 83248 b292151462 a66 cd 881)仅允许本地连接。
运行curl -i http://localhost:9515/status,得到:

HTTP/1.1 200 OK
Content-Length:136
Content-Type:application/json; charset=utf-8
Connection:close

{"sessionId":"","status":0,"value":{"build":{"version":"alpha"},"os":{"arch":"x86_64","name":"Linux","version":"4.9.60-linuxkit-aufs"}}}

所以看起来驱动程序工作得很好。
如果我通过google-chrome-stable --headless --disable-cpu --no-sandbox运行chrome headless,我会得到:

[0125/210641.877388:WARNING:discardable_shared_memory_manager.cc(178)] Less than 64MB of free space in temporary directory for shared memory files: 63
[0125/210641.902689:ERROR:instance.cc(49)] Unable to locate service manifest for metrics
[0125/210641.902756:ERROR:service_manager.cc(890)] Failed to resolve service name: metrics
[0125/210642.031088:ERROR:instance.cc(49)] Unable to locate service manifest for metrics
[0125/210642.031119:ERROR:service_manager.cc(890)] Failed to resolve service name: metrics
[0125/210642.032934:ERROR:gpu_process_transport_factory.cc(1009)] Lost UI shared context.

第一个警告可以通过设置/dev/shm:/dev/shm中的Docker音量或将-shm-size设置为较大的值(高于64 MB)来解决。
其余的错误,如果谷歌,导致我从谷歌Chrome存储库许多错误报告。

z31licg0

z31licg01#

最流行的选项是“Docker Selenium”或“Selenoid”。实现是不同的,但两种解决方案都利用Docker来创建类似于Selenium Grid的测试环境。
我推荐使用“selenoid”,要正确配置它,您可以从以下指南开始:https://www.swtestacademy.com/selenoid-tutorial/
如果您选择“Docker Selenium”,这可能是您的起点:https://www.swtestacademy.com/docker-selenium-tutorial/

wbrvyc0a

wbrvyc0a2#

我使用了Selenium映像,在那里安装了dotnet运行时并使其工作。
下面是我的Dockerfile:

FROM selenium/standalone-chrome:latest AS base
WORKDIR /app

# build and copy dotnet project
FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY ["TestBot/TestBot.csproj", "TestBot/"]
RUN dotnet restore "TestBot/TestBot.csproj"
COPY . .
WORKDIR "/src/TestBot"
RUN dotnet build "TestBot.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "TestBot.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .

# install dotnet
RUN sudo wget https://packages.microsoft.com/config/debian/10/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
RUN sudo dpkg -i packages-microsoft-prod.deb
RUN sudo apt-get update
RUN sudo apt-get install -y dotnet-runtime-5.0

ENTRYPOINT ["dotnet", "TestBot.dll"]

我的C#代码与您的代码类似:

using OpenQA.Selenium.Remote;
using OpenQA.Selenium;
using System;
using OpenQA.Selenium.Chrome;
using System.Drawing;
using OpenQA.Selenium.Firefox;
using System.Threading;

namespace TestBot
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            ChromeOptions Options = new ChromeOptions();

            if (OperatingSystem.IsWindows())
            {
                Options.AddAdditionalCapability("platform", "WIN10", true); // Supported values: "VISTA" (Windows 7), "WIN8" (Windows 8), "WIN8_1" (windows 8.1), "WIN10" (Windows 10), "LINUX" (Linux)
            }
            else
            {
                Options.AddAdditionalCapability("platform", "LINUX", true); // Supported values: "VISTA" (Windows 7), "WIN8" (Windows 8), "WIN8_1" (windows 8.1), "WIN10" (Windows 10), "LINUX" (Linux)
            }
            Options.AddArgument("--headless");

            ChromeDriver driver = new ChromeDriver(Options);
            try
            {
                // driver.Manage().Window.Size = new Size(1920, 1080);
                driver.Navigate().GoToUrl("https://google.com/");
                var test = driver.FindElementByTagName("html").Text;
                Console.WriteLine(test);
            }
            finally
            {
                driver.Quit();
            }
            Console.ReadLine();
        }
    }
}

我使用了以下库:

4ngedf3f

4ngedf3f3#

有一个围绕Docker容器构建的非常简洁的框架,它被用作远程驱动程序位置。
http://aerokube.com/selenoid/latest/
我还没有完全实现它,但我设法毫不费力地创建了docker容器,里面有合适的chrome和firefox驱动程序。

fslejnso

fslejnso4#

以下是Dockerfile RUN命令,用于将Chrome和(匹配的!)Chrome驱动程序安装到映像中。

# install essential tools
RUN apt-get update && apt-get install unzip

# install Chrome
RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb \
    && apt-get install ./google-chrome-stable_current_amd64.deb -y

# download matching Chrome Driver
# https://stackoverflow.com/a/61928952/167920
RUN chromeVersion=$(google-chrome --product-version) \
    && chromeMajorVersion=${chromeVersion%%.*} \
    && latestDriverReleaseURL=https://chromedriver.storage.googleapis.com/LATEST_RELEASE_$chromeMajorVersion \
    && wget $latestDriverReleaseURL \
    && latestDriverVersionFileName="LATEST_RELEASE_"$chromeMajorVersion \
    && latestFullDriverVersion=$(cat $latestDriverVersionFileName) \
    && rm $latestDriverVersionFileName \
    && finalURL="http://chromedriver.storage.googleapis.com/"$latestFullDriverVersion"/chromedriver_linux64.zip" \
    && wget $finalURL \
    && unzip chromedriver_linux64.zip \
    && rm chromedriver_linux64.zip
    
# Copy chromedriver to the desired folder
# mkdir --parents /source/tests/MyProject.Tests/bin/Debug/net6.0 \
# && mv chromedriver /source/tests/MyProject.Tests/bin/Debug/net6.0/

相关问题