1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Selenium】Dockerコンテナ内でSeleniumを実行すると起きるエラーの解決: Selenium::WebDriver::Error::UnknownError【ruby】

Last updated at Posted at 2022-06-15

ソースコード

だいぶはしょってますが、クロールするための、Crawlerクラスです。

require 'selenium-webdriver'

class Crawler
  def execute
    driver.navigate.to start_url
    driver.find_elements(:xpath, '//*[@id="tabular-buybox"]/div[1]/div[4]/div/span/a')
    # 以下略
  end
    
  private

  def driver
    @driver ||= begin
      options = Selenium::WebDriver::Chrome::Options.new
      options.add_argument('--headless')
      Selenium::WebDriver.for(:chrome , options: options).tap do |driver|
        driver.manage.timeouts.implicit_wait = 2
      end
    end
  end
end

問題

Dockerコンテナ内で、上記を動かすと以下のエラーが発生

Selenium::WebDriver::Error::UnknownError
"unknown error: Chrome failed to start: exited abnormally.\n  (unknown error: DevToolsActivePort file doesn't exist)\n  (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)"

解決策

--no-sandboxと、--disable-dev-shm-usageオプションを追加しましょう!

  def driver
    @driver ||= begin
      options = Selenium::WebDriver::Chrome::Options.new
      options.add_argument('--headless')
      options.add_argument('--no-sandbox') # 追加
      options.add_argument('--disable-dev-shm-usage') # 追加
      Selenium::WebDriver.for(:chrome , options: options).tap do |driver|
        driver.manage.timeouts.implicit_wait = 2
      end
    end
  end
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?