pyronlaboratory / heroku-integrated-firefox-geckodriver

Buildpack enables your client code to access Firefox along with Geckodriver in a Heroku slug.
https://pyronlaboratory.github.io/heroku-integrated-firefox-geckodriver/
MIT License
41 stars 81 forks source link

Steps in README do not work on a new created app #12

Open alebian opened 3 years ago

alebian commented 3 years ago

Hi! I recently created a new app in Heroku, installed the buildpack, added the ENV variables and it is not working.

I tried different things commented on other issues (I'm using Ruby):

Selenium::WebDriver::Firefox::Binary.path = ENV['FIREFOX_BIN']
Selenium::WebDriver::Firefox.driver_path = ENV['GECKODRIVER_PATH']
options = Selenium::WebDriver::Firefox::Options.new(args: ['-headless', '--disable-gpu', '--no-sandbox', '--remote-debugging-port=9224'])
browser = Selenium::WebDriver.for(:firefox, options: options)

But I'm getting

Selenium::WebDriver::Error::SessionNotCreatedError (Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line)

My env variables are:

FIREFOX_BIN=/app/vendor/firefox/firefox
GECKODRIVER_PATH=/app/vendor/geckodriver/geckodriver
LD_LIBRARY_PATH=/usr/local/lib:/usr/lib:/lib:/app/vendor
PATH=/usr/local/bin:/usr/bin:/bin:/app/vendor/

I also tried removing the buildpack from the heroku webapp, installing it from the command line and restarting the dynos (as commented https://github.com/pyronlaboratory/heroku-integrated-firefox-geckodriver/issues/3#issuecomment-575932297 but it didn't work)

Thanks!

pyronlaboratory commented 3 years ago

Hello alebian, looks like you're using a deprecated method for setting the path to your Geckodriver.

This is how I bootstrap a ruby app for using selenium

#!/usr/bin/env ruby

require 'bundler/setup'
Bundler.require

require "selenium-webdriver" 

Selenium::WebDriver::Firefox::Binary.path = "/app/vendor/firefox/firefox" #your heroku env variables should reflect this path
puts "Binary file located at: #{Selenium::WebDriver::Firefox::Binary.path}"

Selenium::WebDriver::Firefox::Service.driver_path = "/app/vendor/geckodriver/geckodriver" #your heroku env variables should reflect this path
puts "Geckodriver located at: #{Selenium::WebDriver::Firefox::Service.driver_path}"

Selenium::WebDriver.logger.level = :debug

options = Selenium::WebDriver::Firefox::Options.new(args: ['-headless', '--disable-gpu', '--no-sandbox', '--remote-debugging-port=9224'])

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

driver.get "http://google.com"
puts "Driver discovered webapp: #{driver.title}"
driver.quit

Do post a detailed debug log if you're still facing the error.

pdobb commented 2 years ago

Note:

Selenium::WebDriver::Firefox::Binary.path = "..."

Should (now) be:

Selenium::WebDriver::Firefox.path = "..."

in selenium-webdriver v4.1.0.

Otherwise, this worked for me, thanks @pyronlaboratory.

The problem I've been having is when/where to set these paths in a Rails app. But just running your code in the rails console does work.

UPDATE: A solution that worked for my case is to do this in a Rails initializer file:

# Use `:debug` instead of `:info` for detailed logs in case of an error.
Selenium::WebDriver.logger.level = :warn

if Rails.env.development?
  Capybara.default_driver = :selenium
else # Production
  # Heroku-specific setup for Selenium + Firefox paths, etc.

  Capybara.default_driver = :selenium_headless

  if File.exist?(ENV["FIREFOX_BIN"])
    Selenium::WebDriver::Firefox.path = ENV["FIREFOX_BIN"]
    Selenium::WebDriver::Firefox::Service.driver_path = ENV["GECKODRIVER_PATH"]
  end
end

The key part being if File.exist?(ENV["FIREFOX_BIN"]). This conditional allows the deploys to work without fail, and also is what's needed when running the Rails application.