gem 'webdrivers'
gem 'selenium-webdriver'
curl -sL -o chrome.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb; sudo apt install ./chrome.deb && rm chrome.deb && echo "Done installing Chrome!" || echo "Error installing Chrome!"
mkdir ~/Tools;curl -sL -o chromedriver.zip "https://chromedriver.storage.googleapis.com/$(google-chrome --version | awk '{ print $3 }')/chromedriver_linux64.zip" && unzip chromedriver.zip -d ~/Tools && rm chromedriver.zip && echo "Installed Chromedriver!" || echo "Error installing Chromedriver!"
If you want to deploy your app in Heroku and have the necessary binaries to use Selenium, use the following buildpack.
heroku buildpacks:add --index 1 https://github.com/heroku/heroku-buildpack-google-chrome -a yourappname
Note if you are using the chromedriver buildpack and are running into compatibility issues between chromedriver and Google Chrome, consider switching to the webdrivers
gem instead. Here's a guide on how to do that.
require "selenium-webdriver"
global_timeout = 12 #seconds, max time to load
lookup_target = "https://expired.badssl.com/"
capabilities = ""
options = Selenium::WebDriver::Chrome::Options.new
# Below line needed if you're deploying on Heroku with the Chrome buildpack
Selenium::WebDriver::Chrome.path = ENV['GOOGLE_CHROME_SHIM'] if ENV['GOOGLE_CHROME_SHIM'].present?
options.add_argument "--window-size=1280x800"
options.add_argument "--headless"
options.add_argument "--disable-gpu"
options.add_argument "--disable-speech-api"
options.add_argument "--disable-sync" #Disable Google account syncing
options.add_argument "--disable-translate"
options.add_argument "--disable-web-security" #Don't care about SOP
options.add_argument "--hide-scrollbars"
options.add_argument "--ignore-certificate-errors"
options.add_argument "--no-default-browser-check"
options.add_argument "--no-pings" #"Don't send hyperlink auditing pings"
options.add_argument "--fast"
# --no-sandbox
# --no-startup-window
driver = Selenium::WebDriver.for :chrome, options: options
driver.manage.timeouts.implicit_wait = global_timeout
driver.manage.timeouts.script_timeout = global_timeout
driver.manage.timeouts.page_load = global_timeout
driver.navigate.to(lookup_target)
sleep(4) #wait for dynamic content to load
driver.save_screenshot('screenshot.png')
driver.quit
mkdir ~/Tools;FIREFOX_LATEST_URL="https://download.mozilla.org/?product=firefox-latest-ssl&os=linux64&lang=en-US";curl -sL $FIREFOX_LATEST_URL | tar -C ~/Tools --strip-components=1 -xjf - && export PATH=$PATH:~/Tools
mkdir ~/Tools; curl -L https://github.com$(curl -sL https://github.com/mozilla/geckodriver/releases/latest | grep -i 'linux64.tar.gz\"' | cut -d \" -f 2) | tar -C ~/Tools -xzf - && export PATH=$PATH:~/Tools
This example returns a base-64 encoded image (useful for rendering into a web page).
screenshot_enabled = "1"
def get_screenshot(lookup_target)
global_timeout = 12 #seconds, max time to load
screenshot_base64 = ""
if screenshot_enabled == "1"
begin
capabilities = Selenium::WebDriver::Remote::Capabilities.firefox(accept_insecure_certs: true)
options = Selenium::WebDriver::Firefox::Options.new(args: ['-headless', '-safe-mode'])
driver = Selenium::WebDriver.for :firefox, options: options, desired_capabilities: capabilities
driver.manage.timeouts.implicit_wait = global_timeout
driver.manage.timeouts.script_timeout = global_timeout
driver.manage.timeouts.page_load = global_timeout
driver.navigate.to lookup_target
sleep(2.5) #wait for dynamic content to load
screenshot_base64 = driver.screenshot_as(:base64)
driver.quit
rescue Selenium::WebDriver::Error::TimeoutError => toe
screenshot_base64 = ""
driver.quit unless driver.nil?
rescue Selenium::WebDriver::Error::UnknownError => ue
screenshot_base64 = ""
driver.quit unless driver.nil?
rescue Selenium::WebDriver::Error::WebDriverError => wde
screenshot_base64 = ""
driver.quit unless driver.nil?
end
end
return screenshot_base64
end