如何在Docker Linux容器中运行C#Selenium程序

时间:2018-05-16 20:45:24

标签: c# selenium docker docker-compose dockerfile

使用dotnet工具,我可以构建一个C#项目并使用DLL运行它。

  • dotnet clean
  • dotnet build -c "Release" .
  • dotnet ./SeleniumSample/bin/Release/netcoreapp2.0/SeleniumSample.dll

但是,当我尝试在docker容器中复制它时,我遇到了多个问题:

  1. 加载ChromeDriver
  2. 以无头模式加载Chrome
  3. 到目前为止,我还没有找到组合样本[" C#" ,#34; Selenium"," Docker"]互联网上的任何其他地方。所以,如果你能帮助我,我会很高兴。

    加载Chrome驱动程序的Snipplet:

    ChromeOptions options = new ChromeOptions();
    options.AddArgument("--headless");
    driver = new ChromeDriver(Directory.GetCurrentDirectory(), options);
    

    注意: Directory.GetCurrentDirectory()将指向使用chromedriver.exe cli构建生成的dotnet目录。

    SeleniumSample.csproj 的内容如下所示:

    <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 内容:

    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
    

    执行所创建图像容器的代码位于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
    

    发生以下错误:

    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)
    

    我的猜测是Chrome没有安装在docker镜像中,所以我收到了这个错误。如果是这样,我该如何解决这个问题?

    编辑1 : 将以下行添加到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
    

    现在我收到以下错误 -

    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.
    

    完成此修改后,问题变得与this问题类似,但在网站上仍然值得拥有,因为据我所知,没有其他人尝试过这样的情景。

0 个答案:

没有答案