【问题标题】:How to install firefox add-on using Capybara & Selenium?如何使用 Capybara 和 Selenium 安装 firefox 插件?
【发布时间】:2020-12-05 04:17:23
【问题描述】:

我正在尝试在 Firefox 上安装一个插件,但是,我尝试过的任何方法都无法实现。

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'

我在这里做错了什么?浏览器在不安装任何插件的情况下访问 URL。我可以从文件中手动安装插件。

此外,当我想添加个人资料时,我收到以下错误:

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'

【问题讨论】:

    标签: ruby selenium selenium-webdriver capybara


    【解决方案1】:

    好吧,既然还没有人回答,我将分享我是如何克服这个问题的,以防你想安装扩展程序。

    如果其他人有更好的答案,那么我可以接受他们的答案,所以如果你有更好的解决方案,请回答。

    第一种方法

    我只是在搞乱 Firefox 配置文件文件夹,我想我可以使用一个已经存在的配置文件文件夹,其中只有 extensions 文件夹。所以我已经删除了我想要使用的配置文件文件夹中的所有其他文件/文件夹。

    所以基本上步骤是:

    • 创建 Firefox 配置文件或使用现有配置文件。
    • 使用具有相同配置文件的浏览器安装所需的插件。
    • 关闭浏览器。
    • 导航到同一个 Firefox 配置文件夹并删除配置文件夹中的所有其他文件/文件夹。
    • 然后在实例化 Selenium::WebDriver::Firefox::Profile 时使用该 Firefox 配置文件文件夹的路径,如下所示:
    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'
    

    我尝试创建一个名为 profile 的文件夹,并在该文件夹内创建了另一个名为 extensions 的文件夹,就像 Firefox 所做的那样,并将我想要上传的所有附加组件移动到该 extensions文件夹,但没有用。

    我猜 Firefox 在安装插件时会更改插件文件,因此从 their website 下载插件并尝试在配置文件文件夹中使用它是行不通的。这当然是我的猜测。

    第二种方法

    您也可以使用:

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

    我知道这看起来很乱,所以你可以停止使用 Capybara 并直接使用 seleniumCapybara 也有一些很酷的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多