module Browsed module Firefox private def register_firefox_driver(options: {}) open_timeout = options.fetch(:open_timeout, 60) read_timeout = options.fetch(:read_timeout, 60) browser_id = options.fetch(:browser_id, nil) download_path = options.fetch(:download_path, self.configuration.download_path) private_browsing = options.fetch(:private_browsing, false) profile = Selenium::WebDriver::Firefox::Profile.new if private_browsing profile["browser.privatebrowsing.autostart"] = true end unless download_path.to_s.empty? profile["browser.download.useDownloadDir"] = true profile["browser.download.dir"] = download_directory profile["browser.download.folderList"] = 2 profile["browser.helperApps.neverAsk.saveToDisk"] = "text/plain, application/vnd.ms-excel, text/csv, text/comma-separated-values, application/octet-stream, text/x-comma-separated-values, application/csv, application/x-filler" profile["toolkit.telemetry.prompted"] = true profile["pdfjs.disabled"] = true end profile["browser.tabs.warnOnClose"] = false profile["browser.tabs.warnOnCloseOtherTabs"] = false profile["general.useragent.override"] = self.user_agent unless self.user_agent.to_s.empty? id = SecureRandom.hex[0..15] profile = firefox_proxy_options(profile) options = Selenium::WebDriver::Firefox::Options.new(profile: profile) options.args << "--browser_id=#{id}" unless browser_id.to_s.empty? options.args << "--headless" if headless? Capybara.register_driver self.driver do |app| client = Selenium::WebDriver::Remote::Http::Default.new(open_timeout: open_timeout, read_timeout: read_timeout) Capybara::Selenium::Driver.new(app, browser: :firefox, http_client: client, options: options) end return id end def firefox_proxy_options(profile) if self.proxy && !self.proxy.empty? && self.proxy.has_key?(:host) && self.proxy.has_key?(:port) proxy_config = Selenium::WebDriver::Proxy.new( http: "#{self.proxy.fetch(:host)}:#{self.proxy.fetch(:port)}", ssl: "#{self.proxy.fetch(:host)}:#{self.proxy.fetch(:port)}", socks: "#{self.proxy.fetch(:host)}:#{self.proxy.fetch(:port)}" ) proxy_config.socks_username = self.proxy.fetch(:username) unless self.proxy.fetch(:username, nil).to_s.empty? proxy_config.socks_password = self.proxy.fetch(:password) unless self.proxy.fetch(:password, nil).to_s.empty? profile.proxy = proxy_config log("Will use proxy #{self.proxy.fetch(:host)}:#{self.proxy.fetch(:port)} to initiate the request.") end return profile end end end