ソースコード
だいぶはしょってますが、クロールするための、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