0

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:

  1. Loading ChromeDriver
  2. 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.

Naveen Dennis
  • 1,223
  • 3
  • 24
  • 39
  • If I had to guess, based on the error message, it is looking for the linux chrome binary which is not chromedriver.exe. I would try with the linux binary here:http://chromedriver.chromium.org/downloads – John Koerner May 16 '18 at 20:52
  • But if I wanted to run this on a Continuous Integration Server or Simply on Docker itself then it should be able to execute without hard coded links to the local machine, right? – Naveen Dennis May 16 '18 at 20:54
  • I believe if the driver was not found, I would have gotten an error like this, right? `Unhandled Exception: OpenQA.Selenium.DriverServiceNotFoundException: The chromedriver file does not exist in the current directory or in a directory on the PATH environment variable.` – Naveen Dennis May 16 '18 at 20:57
  • No @John, I did not install chrome using Dockerfile but I did launch into the image after removing the `ENTRYPOINT` in `Dockerfile` and tried to install chrome using the instructions provided here (https://askubuntu.com/a/510186/270827) and then ran `dotnet ./SeleniumSample.dll` on it but that did not solve the problem – Naveen Dennis May 16 '18 at 21:04
  • Possible duplicate of https://stackoverflow.com/questions/22322596/selenium-error-the-http-request-to-the-remote-webdriver-timed-out-after-60-sec?noredirect=1&lq=1 – Naveen Dennis May 17 '18 at 16:10
  • Wrong question was marked as the redirect link please look at the one above for the solution – Naveen Dennis May 17 '18 at 16:12

0 Answers0