Starting chromedriver from rails app gives unable to connect to /usr/bin/chromedriver 127.0.0.1:9515

I’ve been debugging this for quite some time and I don’t know what else I should do to fix this.

Long story short: I have a rails app that needs to launch chromedriver to navigate to a specific website and get some data from there. I’m using Ubuntu and 22.04 and I installed chromedriver and google chrome stable on /usr/bin. I can launch chrome and chromedriver from the terminal. My rails app is consuming from a sidekiq queue the URL to navigate and the application fails with:

Selenium::WebDriver::Error::WebDriverError: unable to connect to /usr/bin/chromedriver 127.0.0.1:9515

It seems an issue launching chromedriver from rails app, but I did already some debug steps.

To debug this I created a standalone ruby script and executed a similar code to navigate to google and get the page title. It ran successfully. After that I created this code and added to config/initializers/selenium.rb

# config/initializers/selenium.rb

require 'selenium/webdriver'

Rails.logger.info "==== Boot up context ===="
Rails.logger.info "Rails environment: #{Rails.env}"
Rails.logger.info "User executing: #{ENV['USER']}"
Rails.logger.info "PATH: #{ENV['PATH']}"

user_agent = 'Mozilla/5.0 (X11'

args = ["--user-agent=#{user_agent}", '--no-sandbox', '--disable-dev-shm-usage', '--ignore-certificate-errors', '--window-size=1920,1080']
args << "--headless=new" if Rails.env.production?

options = Selenium::WebDriver::Chrome::Options.new(
    args: args
)

begin
    Rails.logger.info "Initializing ChromeDriver"
    Rails.logger.info "Chrome options: #{options.args.join(' ')}"

    driver = Selenium::WebDriver.for(:chrome, options: options)

    driver.navigate.to "http://www.google.com"
    Rails.logger.info "Page title: #{driver.title}"
    driver.quit
rescue => e
    Rails.logger.error "Error starting chromedriver: #{e.message}"
    Rails.logger.error e.backtrace.join("n")
end

When the app boot, I can see the page title with success. So far so good!

The problem is when I add this to my main code when app get from the queue. The main difference is that I have a Module, don’t know if that makes any relevant difference for this case but I’m not able to launch chromedriver and it fails with the error Selenium::WebDriver::Error::WebDriverError: unable to connect to /usr/bin/chromedriver 127.0.0.1:9515

I will post the code below

require 'selenium-webdriver'

module MyModule
  PROXY_ENABLED = true
  USER_AGENT = 'Mozilla/5.0'

  def proxy_enabled?
    PROXY_ENABLED
  end

  def proxy_username
    ProxyManager.instance.username
  end

  def proxy_password
    ProxyManager.instance.password
  end

  def random_proxy
    ProxyManager.instance.random_proxy(self.class::SOURCE_NAME)
  end

  def user_agent
    USER_AGENT
  end

  def build_selenium
    proxy_addr = proxy_enabled? ? random_proxy : nil

    proxy = proxy_enabled? ? Selenium::WebDriver::Proxy.new(
      http: proxy_addr,
      ssl: proxy_addr
    ) : nil

    args = ["--user-agent=#{user_agent}", '--no-sandbox', '--disable-dev-shm-usage', '--ignore-certificate-errors', '--window-size=1920,1080']
    args << "--headless=new" if Rails.env.production?

    options = Selenium::WebDriver::Chrome::Options.new(
      proxy: proxy,
      args: args
    )

    driver = Selenium::WebDriver.for(:chrome, options: options)

    if proxy_enabled?
      driver.register(username: proxy_username, password: proxy_password)
    end
    
    # Return a hash containing both the driver, the proxy address, and the source name
    {
      driver: driver,
      proxy_addr: proxy_addr
    }
  end

I printed the PATH and running user and they are the same on both situations. I also printed chromedriver and google-chrome paths and they looks ok.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật