Using dotnet
tools, I can build a C# project and run it by using the DLL.
dotnet clean
dotnet build -c "Release" .
dotnet ./SeleniumSample/bin/Release/netcoreapp2.0/SeleniumSample.dll
However, when I try to replicate this in a docker container, I am faced with multiple issues:
- Loading ChromeDriver
- Loading Chrome in headless mode
So far, I have not found samples of the combination ["C#" , "Selenium", "Docker"] anywhere else in the internet. So, I would love it if you could help me.
Snipplet of loading the Chrome Driver:
ChromeOptions options = new ChromeOptions();
options.AddArgument("--headless");
driver = new ChromeDriver(Directory.GetCurrentDirectory(), options);
Note: Directory.GetCurrentDirectory()
will point to the directory containing chromedriver.exe
generated by the build using dotnet
cli.
Contents of SeleniumSample.csproj is shown below:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<Configurations>Debug;Release;ALL</Configurations>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Selenium.Firefox.WebDriver" Version="0.20.0" />
<PackageReference Include="Selenium.WebDriver" Version="3.12.0" />
<PackageReference Include="Selenium.WebDriver.ChromeDriver" Version="2.38.0.1" />
</ItemGroup>
</Project>
Dockerfile contents:
FROM microsoft/dotnet
ARG source
WORKDIR /app
COPY ${source} .
RUN dotnet clean
RUN dotnet build -c "Release"
WORKDIR ./SeleniumSample/bin/Release/netcoreapp2.0/
ENTRYPOINT dotnet ./SeleniumSample.dll
Code to execute the container of the created image is below after docker-compose up
:
docker run -i -t -P -v /lib/x86_64-linux-gnu/libglib-2.0.so.0:/lib/x86_64-linux-gnu/libglib-2.0.so.0 \
-v /usr/lib/x86_64-linux-gnu/libX11.so.6:/usr/lib/x86_64-linux-gnu/libX11.so.6 \
-v /usr/lib/x86_64-linux-gnu/libnss3.so:/usr/lib/x86_64-linux-gnu/libnss3.so \
-v /usr/lib/x86_64-linux-gnu/libnssutil3.so:/usr/lib/x86_64-linux-gnu/libnssutil3.so \
-v /usr/lib/x86_64-linux-gnu/libnspr4.so:/usr/lib/x86_64-linux-gnu/libnspr4.so \
-v /usr/lib/x86_64-linux-gnu/libxcb.so.1:/usr/lib/x86_64-linux-gnu/libxcb.so.1 \
-v /usr/lib/x86_64-linux-gnu/libplc4.so:/usr/lib/x86_64-linux-gnu/libplc4.so \
-v /usr/lib/x86_64-linux-gnu/libplds4.so:/usr/lib/x86_64-linux-gnu/libplds4.so \
-v /usr/lib/x86_64-linux-gnu/libXau.so.6:/usr/lib/x86_64-linux-gnu/libXau.so.6 \
-v /usr/lib/x86_64-linux-gnu/libXdmcp.so.6:/usr/lib/x86_64-linux-gnu/libXdmcp.so.6 \
seleniumsample_selenium-sample
The following error occurs:
Starting ChromeDriver 2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb) on port 40599
Only local connections are allowed.
Unhandled Exception: OpenQA.Selenium.WebDriverException: unknown error: cannot find Chrome binary
(Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Linux 4.13.0-41-generic x86_64)
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)
at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout)
at SeleniumSample.Utility.GetDriver(String browserName, String argument) in /app/SeleniumSample/Utility.cs:line 54
at SeleniumSample.GmailUnreadEmails.Launcher() in /app/SeleniumSample/GmailUnreadEmails.cs:line 80
at SeleniumSample.Launcher.Main(String[] args) in /app/SeleniumSample/Launcher.cs:line 8
Aborted (core dumped)
My guess is the Chrome
is not installed in the docker image and so I am getting this error. If that is true, how do I fix this problem?
EDIT 1: after adding the below lines to the Dockerfile
RUN \
apt-get update && \
apt-get install -y wget
RUN \
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - && \
echo "deb http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google.list && \
apt-get update && \
apt-get install -y google-chrome-stable && \
rm -rf /var/lib/apt/lists/*
WORKDIR /data
EXPOSE 5901
Now I get the below error-
selenium-sample_1 | Only local connections are allowed.
selenium-sample_1 |
selenium-sample_1 | Unhandled Exception: OpenQA.Selenium.WebDriverException: The HTTP request to the remote WebDriver server for URL http://localhost:45521/session timed out after 60 seconds. ---> System.Net.WebException: The operation has timed out.
After this edit, the question becomes similar to this question but it is still worth having on the site because no one else has tried such a scenario to the best of my knowledge.