tl;博士
如何安装所有组件以在 docker 容器中运行 Selenium?
问题
我从这张图片开始:
FROM microsoft/aspnetcore-build:2 AS builder
WORKDIR /source
COPY . .
RUN dotnet restore
RUN dotnet build
ENTRYPOINT ["dotnet", "run"]
我怎样才能让它启动和使用 headless 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));
在 docker 容器中?
我尝试了什么
安装 Chrome 驱动程序
chromedriver
通过 Selenium.WebDriver.ChromeDriver
NuGet 包分发。
安装 Chrome
在我安装了 Google Chrome 的 Mac OS X 上,当前设置运行良好。
我尝试添加这些行:
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 来解决出现的每个错误。
如果我运行这个设置,我会得到:
Starting ChromeDriver 2.35.528139 (47ead77cb35ad2a9a83248b292151462a66cd881) on port 57889 Only local connections are allowed. An error occurred while sending the request. Couldn't connect to
运行容器时。
在容器中调试
如果我手动进入容器并尝试手动运行 chrome 驱动程序,我会得到:
Starting ChromeDriver 2.35.528139 (47ead77cb35ad2a9a83248b292151462a66cd881) on port 9515 Only local connections are allowed.
并运行 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
设置为更大的值(更高大于 64MB)。
其余的错误,如果 google 的话,会导致我从 Google Chrome 存储库中报告许多错误。
最佳答案
最受欢迎的选项是“docker selenium”或“selenoid”。实现方式不同,但两种解决方案都利用 docker 创建类似于 selenium 网格的测试环境。
我推荐“selenoid”并正确配置它,您可以从以下指南开始:https://www.swtestacademy.com/selenoid-tutorial/
如果您选择“docker selenium”,这可能是您的起点:https://www.swtestacademy.com/docker-selenium-tutorial/
关于c# - 如何在 docker 容器中运行 selenium chrome 驱动程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48463976/