.net 5 Selenium webdriver not working in docker container error : No such file or directory

.net 5 Selenium webdriver not working in docker container error : No such file or directory

我对 .net 的 selenium webdriver 有问题需要你的帮助。

我正在使用 .net 5 并尝试在 Linux Docker 容器上 运行 Selenium Webdriver 并获取“System.ComponentModel.Win32Exception 没有这样的文件或目录“ 错误。我以为 chromedriver 文件不存在。但是我已经检查过路径是正确的,即使我把chromeriver放在usr/local/bin,app/文件夹。

当我 运行 没有 docker 时它工作。

我的代码:

...
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
....
var chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("headless");
chromeOptions.AddArgument("no-sandbox");
chromeOptions.PageLoadStrategy = PageLoadStrategy.Eager;

// var driver = new ChromeDriver(chromeOptions);
// var driver = new ChromeDriver("/usr/local/bin/",chromeOptions); 
var driver = new ChromeDriver(AppDomain.CurrentDomain.BaseDirectory, chromeOptions);

错误:

An unhandled exception has occurred while executing the request.
      System.ComponentModel.Win32Exception (2): No such file or directory
         at System.Diagnostics.Process.ForkAndExecProcess(String filename, String[] argv, String[] envp, String cwd, Boolean redirectStdin, Boolean redirectStdout, Boolean redirectStderr, Boolean setCredentials, UInt32 userId, UInt32 groupId, UInt32[] groups, Int32& stdinFd, Int32& stdoutFd, Int32& stderrFd, Boolean usesTerminal, Boolean throwOnNoExec)
         at System.Diagnostics.Process.StartCore(ProcessStartInfo startInfo)
         at System.Diagnostics.Process.Start()
         at OpenQA.Selenium.DriverService.Start()
         at OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute)
         at OpenQA.Selenium.WebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
         at OpenQA.Selenium.WebDriver.StartSession(ICapabilities desiredCapabilities)
         at OpenQA.Selenium.WebDriver..ctor(ICommandExecutor executor, ICapabilities capabilities)
         at OpenQA.Selenium.Chromium.ChromiumDriver..ctor(ChromiumDriverService service, ChromiumOptions options, TimeSpan commandTimeout)
         at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout)
         at OpenQA.Selenium.Chrome.ChromeDriver..ctor(String chromeDriverDirectory, ChromeOptions options, TimeSpan commandTimeout)
         at OpenQA.Selenium.Chrome.ChromeDriver..ctor(String chromeDriverDirectory, ChromeOptions options)

我的docker文件

# install chromedriver
 RUN apt-get install -yqq unzip
 RUN wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip
 RUN unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/

# Install Chrome WebDriver
RUN CHROMEDRIVER_VERSION=94.0.4606.61 && \
    mkdir -p /opt/chromedriver-$CHROMEDRIVER_VERSION && \
    curl -sS -o /tmp/chromedriver_linux64.zip http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_mac64_m1.zip && \
    unzip -qq /tmp/chromedriver_linux64.zip -d /opt/chromedriver-$CHROMEDRIVER_VERSION && \
    rm /tmp/chromedriver_linux64.zip && \
   chmod +x /opt/chromedriver-$CHROMEDRIVER_VERSION/chromedriver && \
    ln -fs /opt/chromedriver-$CHROMEDRIVER_VERSION/chromedriver /usr/local/bin/chromedriver && \
    chmod +x /usr/local/bin/chromedriver

包裹信息:

.Net 5
Selenium.WebDriver --version 4.0.1
ChromeDriver version 94.0.4606.41 
Operating System: Ubuntu 20.

我正在使用远程驱动程序而不是 chrome 文件。