简体   繁体   English

如何在 docker 容器中运行 selenium chrome 驱动程序?

[英]How can I run selenium chrome driver in a docker container?

tl;dr tl;博士

How can I install all the components to run Selenium in a docker container?如何安装所有组件以在 docker 容器中运行 Selenium?


Question问题

I'm starting with this image:我从这张图片开始:

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

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

How can I make it so that I can start and use an headless Chrome Driver with this:我该怎么做才能启动和使用无头 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));

within a docker container?在 docker 容器内?


What have I tried我试过什么

Installing the Chrome driver安装 Chrome 驱动程序

The chromedriver is distributed via the Selenium.WebDriver.ChromeDriver NuGet package. chromedriver通过Selenium.WebDriver.ChromeDriver NuGet package 分发。

Installing Chrome安装 Chrome

On my Mac OS X with Google Chrome installed the current setup works just fine.在我安装了 Google Chrome 的 Mac OS X 上,当前设置工作正常。

I've tried to add these lines:我试图添加这些行:

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

The above installs this version of Chrome:上面安装了这个版本的 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

which is compatible with the version of the Chrome Driver.与 Chrome 驱动程序的版本兼容。

which come from trying to solve each error that came out by trying to run Selenium with the docker container.这是通过尝试使用 docker 容器运行 Selenium 来尝试解决出现的每个错误。

If I run this setup I get:如果我运行这个设置,我会得到:

Starting ChromeDriver 2.35.528139 (47ead77cb35ad2a9a83248b292151462a66cd881) on port 57889 Only local connections are allowed.在端口 57889 上启动 ChromeDriver 2.35.528139 (47ead77cb35ad2a9a83248b292151462a66cd881) 仅允许本地连接。 An error occurred while sending the request.发送请求时出错。 Couldn't connect to无法连接到

when running the container.运行容器时。

Debugging in the container在容器中调试

If I enter the container manually and try to run the chrome driver manually I get:如果我手动输入容器并尝试手动运行 chrome 驱动程序,我会得到:

Starting ChromeDriver 2.35.528139 (47ead77cb35ad2a9a83248b292151462a66cd881) on port 9515 Only local connections are allowed.在端口 9515 上启动 ChromeDriver 2.35.528139 (47ead77cb35ad2a9a83248b292151462a66cd881) 仅允许本地连接。

and running curl -i http://localhost:9515/status I get:并运行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"}}}

so it seems that the driver works just fine.所以看起来驱动程序工作得很好。

If I run chrome headless instead via google-chrome-stable --headless --disable-cpu --no-sandbox I get:如果我通过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.

The first warning can be solved via setting a docker volume in /dev/shm:/dev/shm or by setting -shm-size to something large (higher than 64MB).第一个警告可以通过在/dev/shm:/dev/shm中设置 docker 卷或将-shm-size设置为较大的值(高于 64MB)来解决。

The rest of the errors, if google, lead to me many bug reports from Google Chrome repositories. rest 的错误,如果是谷歌,会导致我从 Google Chrome 存储库中收到许多错误报告。

The most popular options are "docker selenium" or "selenoid".最受欢迎的选项是“docker selenium”或“selenoid”。 The implementation is different but both solutions take advantage of docker to create test environment similar to selenium grid.实现是不同的,但两种解决方案都利用 docker 来创建类似于 selenium grid 的测试环境。

I recommend "selenoid" and to configure it properly you could start with the following guide: https://www.swtestacademy.com/selenoid-tutorial/我推荐“selenoid”并正确配置它,您可以从以下指南开始: https ://www.swtestacademy.com/selenoid-tutorial/

If you choose "docker selenium" this could be your starting point: https://www.swtestacademy.com/docker-selenium-tutorial/如果您选择“docker selenium”,这可能是您的起点: https : //www.swtestacademy.com/docker-selenium-tutorial/

I used the Selenium image, installed dotnet runtime there and got it working.我使用了 Selenium 映像,在那里安装了 dotnet 运行时并使其正常工作。
Here is my Dockerfile:这是我的 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"]

My C# code looks similar to yours:我的 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();
        }
    }
}

I used the following Libraries:我使用了以下库: 在此处输入图片说明

There's a pretty neat framework built around Docker containers being used as Remote Driver locations.有一个非常简洁的框架围绕着用作远程驱动程序位置的 Docker 容器构建。

http://aerokube.com/selenoid/latest/ http://aerokube.com/selenoid/latest/

I haven't fully implemented it yet, but I managed to effortlessly create docker containers with appropriate chrome and firefox drivers inside.我还没有完全实现它,但我设法毫不费力地创建了 docker 容器,里面有适当的 chrome 和 firefox 驱动程序。

Here are the Dockerfile RUN commands to install Chrome and (a matching.) Chrome Driver into an image.以下是 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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何增加 selenium chrome 驱动程序的页面加载超时 - How can i increase page load timeout of selenium chrome driver 无法在 Selenium 中启动 Chrome 驱动程序 - Can't launch chrome driver in Selenium 为什么硒铬驱动程序在后台运行(无头模式) - Why selenium chrome driver run in the background (headless mode) Wny无法控制Internet断开连接下的Chrome Selenium Web驱动程序[WebDriver Exception] - Wny can't I control Chrome Selenium Web Driver under Internet disconnect [WebDriver Exception] 如何在 selenium 远程 Web 驱动程序中更改用户代理 - How can I change user agent in selenium remote web driver 如何在chrome驱动程序错误行为中通过xpath修复FindElement? - How can I fix FindElement by xpath in chrome driver misbehavior? 如何使用 Selenium Chrome 驱动程序在 chrome 中打开多个 web 页面 - How to open multiple web pages in chrome using Selenium Chrome Driver 如何使用 C# 中的 Selenium chrome 驱动程序从网页打印 URL 列表以进行控制台? - How do I print a list of URLs to console from a webpage using Selenium chrome driver in C#? 我可以进一步减少对由docker容器上的asp.net Web API应用程序运行的docker映像的依赖性 - Can I further reduce dependency on docker images to run by asp.net web api application on docker container 无法使用C#在Selenium上使用已保存的配置文件启动Chrome驱动程序 - Can't start Chrome Driver with saved profile on Selenium with C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM