Selenium web driverexception:未知错误:Chrome无法启动:异常退出

发布于 2025-02-13 10:04:32 字数 3021 浏览 2 评论 0原文

我确实有一个WebJob(在Docker Ubuntu上运行)可以打开一个URL,并使用Selenium和Chromedriver获取HTML结果(字符串)。它在我的开发机(Windows 10)中运行良好,但是当涉及Linux Ubuntu 20时,它不起作用。我已经尝试了几种配置,并且我在网络周围看到了几种配置,但是我曾经曾经是Linux。错误消息是:

OpenQA.Selenium.WebDriverException: unknown error: Chrome failed to start: exited abnormally.
(chrome not reachable)
(The process started from chrome location /driver/ is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

这是我的Dockerfile的运行部分:

# run
FROM mcr.microsoft.com/dotnet/aspnet:6.0-focal-amd64 as runtime

RUN apt update && apt install -y sudo unzip
RUN sudo apt install -y wget

RUN wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.focal_amd64.deb
RUN sudo apt install -y ./wkhtmltox_0.12.6-1.focal_amd64.deb
RUN sudo apt install -y ttf-mscorefonts-installer

RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN sudo apt install -y ./google-chrome-stable_current_amd64.deb

RUN mkdir driver
RUN mkdir userprofile
RUN wget -q --continue -P driver https://chromedriver.storage.googleapis.com/103.0.5060.53/chromedriver_linux64.zip
RUN unzip ./driver/chromedriver* -d ./driver
ENV PATH=$PATH:/driver

WORKDIR /app

COPY --from=build /app/src/WebJobPDF/publish ./

ENTRYPOINT ["./WebJobPDF"]

这是我的C#代码:

      ChromeOptions options = new ChromeOptions();
      options.BinaryLocation = (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "/driver/" : null);
      options.AddArgument("--headless");

      if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
      {
        options.AddArgument("--no-sandbox");
        options.AddArgument("--user-data-dir=/userprofile");
        options.AddArgument("--disable-setuid-sandbox");
        options.AddArgument("--remote-debugging-port=9222");
        options.AddArgument("--disable-dev-shm-usage");
        options.AddArgument("--disable-gpu");
      }

      using (var driver = new ChromeDriver(options))
      {
        driver.Manage().Window.Size = new System.Drawing.Size(1920, 1080);
        driver.Navigate().GoToUrl(url);
        driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(15);
        Int64 pageHeight = (Int64)driver.ExecuteScript("return document.body.scrollHeight");

        for (int i = 0; i < pageHeight; i += 100)
        {
          driver.ExecuteScript($"window.scrollBy(0, 100)");
        }

        driver.ExecuteScript($"document.getElementsByTagName('header')[0].remove();");
        await Task.Delay(2000);
        driver.ExecuteScript($"document.getElementsByTagName('footer')[0].remove();");
        await Task.Delay(2000);
        driver.ExecuteScript(@"document.querySelectorAll(""button[aria-label='Gerar PDF']"")[0].remove();");
        await Task.Delay(2000);
        driver.ExecuteScript(@"document.querySelectorAll(""div[role='alert']"")[0].remove();");

        result = driver.PageSource;
      }

您是否对Ubuntu上缺少的内容有一个想法?

I do have a WebJob (running on docker ubuntu) to open an URL and get the html result (string) using Selenium and ChromeDriver. It runs fine in my dev machine (Windows 10), but when it comes to Linux Ubuntu 20 it doesn't work. I've tried several configs and args I've seen all around the web but I'm not that used to Linux. Error message is:

OpenQA.Selenium.WebDriverException: unknown error: Chrome failed to start: exited abnormally.
(chrome not reachable)
(The process started from chrome location /driver/ is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

This is the running part of my dockerfile:

# run
FROM mcr.microsoft.com/dotnet/aspnet:6.0-focal-amd64 as runtime

RUN apt update && apt install -y sudo unzip
RUN sudo apt install -y wget

RUN wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.focal_amd64.deb
RUN sudo apt install -y ./wkhtmltox_0.12.6-1.focal_amd64.deb
RUN sudo apt install -y ttf-mscorefonts-installer

RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN sudo apt install -y ./google-chrome-stable_current_amd64.deb

RUN mkdir driver
RUN mkdir userprofile
RUN wget -q --continue -P driver https://chromedriver.storage.googleapis.com/103.0.5060.53/chromedriver_linux64.zip
RUN unzip ./driver/chromedriver* -d ./driver
ENV PATH=$PATH:/driver

WORKDIR /app

COPY --from=build /app/src/WebJobPDF/publish ./

ENTRYPOINT ["./WebJobPDF"]

And this is my C# code:

      ChromeOptions options = new ChromeOptions();
      options.BinaryLocation = (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "/driver/" : null);
      options.AddArgument("--headless");

      if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
      {
        options.AddArgument("--no-sandbox");
        options.AddArgument("--user-data-dir=/userprofile");
        options.AddArgument("--disable-setuid-sandbox");
        options.AddArgument("--remote-debugging-port=9222");
        options.AddArgument("--disable-dev-shm-usage");
        options.AddArgument("--disable-gpu");
      }

      using (var driver = new ChromeDriver(options))
      {
        driver.Manage().Window.Size = new System.Drawing.Size(1920, 1080);
        driver.Navigate().GoToUrl(url);
        driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(15);
        Int64 pageHeight = (Int64)driver.ExecuteScript("return document.body.scrollHeight");

        for (int i = 0; i < pageHeight; i += 100)
        {
          driver.ExecuteScript(
quot;window.scrollBy(0, 100)");
        }

        driver.ExecuteScript(
quot;document.getElementsByTagName('header')[0].remove();");
        await Task.Delay(2000);
        driver.ExecuteScript(
quot;document.getElementsByTagName('footer')[0].remove();");
        await Task.Delay(2000);
        driver.ExecuteScript(@"document.querySelectorAll(""button[aria-label='Gerar PDF']"")[0].remove();");
        await Task.Delay(2000);
        driver.ExecuteScript(@"document.querySelectorAll(""div[role='alert']"")[0].remove();");

        result = driver.PageSource;
      }

Do you have an idea about what's missing on Ubuntu for this to run?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慢慢从新开始 2025-02-20 10:04:32

在Linux上运行时,我使用Firefox(Geckodriver)使其正常工作:

if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
      {
        FirefoxOptions optionsFF = new FirefoxOptions();
        optionsFF.AddArgument("--headless");

        using (var driverff = new FirefoxDriver("/usr/bin", optionsFF))
        {
          driverff.Manage().Window.Size = new System.Drawing.Size(1920, 1080);
          driverff.Navigate().GoToUrl(url);
          driverff.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(15);
          Int64 pageHeight = (Int64)driverff.ExecuteScript("return document.body.scrollHeight");

          for (int i = 0; i < pageHeight; i += 100)
          {
            driverff.ExecuteScript($"window.scrollBy(0, 100)");
          }
          driverff.ExecuteScript(@"document.querySelectorAll(""div[role='alert']"")[0].remove();");

          result = driverff.PageSource;
        }

在Windows Chromedriver上运行时工作正常。

现在唯一的问题是WKHTMLTOPDF似乎在HTTPS图像中不起作用。我仍然没有解决方案。

I made it working using Firefox (GeckoDriver) when running on Linux:

if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
      {
        FirefoxOptions optionsFF = new FirefoxOptions();
        optionsFF.AddArgument("--headless");

        using (var driverff = new FirefoxDriver("/usr/bin", optionsFF))
        {
          driverff.Manage().Window.Size = new System.Drawing.Size(1920, 1080);
          driverff.Navigate().GoToUrl(url);
          driverff.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(15);
          Int64 pageHeight = (Int64)driverff.ExecuteScript("return document.body.scrollHeight");

          for (int i = 0; i < pageHeight; i += 100)
          {
            driverff.ExecuteScript(
quot;window.scrollBy(0, 100)");
          }
          driverff.ExecuteScript(@"document.querySelectorAll(""div[role='alert']"")[0].remove();");

          result = driverff.PageSource;
        }

When running on Windows ChromeDriver works fine.

The only problem now is wkhtmltopdf seems to be NOT working for HTTPS images. I still don't have a solution for this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文