How to install firefox add-on using Capybara & Selenium?

Viewed 512

I am trying to install an add-on on Firefox, however, I couldn't achieve it with whatever I've tried.

require 'capybara'
require 'selenium-webdriver'

Capybara.register_driver :selenium_proxy do |app|
  desired_caps = Selenium::WebDriver::Remote::Capabilities.firefox
  options = Selenium::WebDriver::Firefox::Options.new
  profile = Selenium::WebDriver::Firefox::Profile.new

  # Here is the add-on I am trying to install.
  profile.add_extension('/home/user/Downloads/try_xpath-1.3.5-fx.xpi') 
  
  options.profile = profile
  Capybara::Selenium::Driver.new(app, {
    browser: :firefox,
    desired_capabilities: desired_caps,
    options: options
  })
end

browser = Capybara::Session.new(:selenium_proxy)
browser.visit 'https://google.com'

What am I doing wrong here? The browser visits the URL without installing any add-on. I am able to install an add-on manually from the file.

Moreover, when I want to add a profile I get the error below:

Errno::ENOENT: No such file or directory @ rb_file_s_stat - /tmp/webdriver-rb-profilecopy20200815-25523-ie6apk/lock
from /home/burak/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubyzip-2.2.0/lib/zip/entry.rb:405:in `stat'
1 Answers

Well, since noone has answered yet I will share how I've overcome this problem in case you want to install extensions.

If anyone else has a better answer then I can accept their answer, so please answer if you have a better solution.

1st Method

I was just messing around with the Firefox profile folders and I figured that I could use an already existing profile folder with only the extensions folder in it. So I've deleted all the other files/folders in the profile folder that I wanted to use.

So basically the steps are:

  • Create a Firefox profile or use an existing one.
  • Install your desired add-on using the browser with the same profile.
  • Close the browser.
  • Navigate to the same Firefox profile folder and delete all the other files/folders in the profile folder.
  • Then use the path to that Firefox profile folder when you are instantiating a Selenium::WebDriver::Firefox::Profile like so:
Capybara.register_driver :selenium_proxy do |app|
  desired_caps = Selenium::WebDriver::Remote::Capabilities.firefox
  options = Selenium::WebDriver::Firefox::Options.new
  profile = Selenium::WebDriver::Firefox::Profile.new '/path/to/firefox/profile/folder/'
  
  options.profile = profile
  Capybara::Selenium::Driver.new(app, {
    browser: :firefox,
    desired_capabilities: desired_caps,
    options: options
  })
end

browser = Capybara::Session.new(:selenium_proxy)
browser.visit 'https://google.com'

I've tried to create a folder named profile and inside that folder I've created another folder named extensions the same way Firefox does and moved all the add-ons I wanted to upload into that extensions folder but that didn't work.

I guess Firefox changes the add-on file when installing the add-on so downloading an add-on from their website and trying to use it in the profile folder doesn't work. This is my guess of course.

2nd Method

You could alternatively use:

browser = Capybara::Session.new(:selenium_proxy)
browser.driver.browser.install_addon '/path/to/addon.xpi'

I know this looks messy so you could just stop using Capybara and use selenium directly but Capybara has some cool methods as well.

Related