如何使用Capybara和Selenium安装Firefox附加组件? 第一种方法第二种方法

问题描述

我正在尝试在Firefox上安装附加组件,但是无论如何我都无法实现。

<ListBox ItemsSource="{Binding TankItems}" 
         SelectedItem="{Binding SelectedTankItem}">
  <ListBox.ItemContainerStyle>
    <Style targettype="ListBoxItem">
      <Setter Property="IsSelected" 
              Value="{Binding IsSelected,Mode=TwoWay}" />
    </Style>
  </ListBox.ItemContainerStyle>
  <ListBox.ItemTemplate>
    <DataTemplate DataType="{x:Type Contactviewmodel}">
      <StackPanel>
        <TextBlock Text="Test" />
      </StackPanel>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

<Border>
  <TextBlock Text="{Binding displayPopup}" />
</Border>

在这里做错了什么?浏览器无需安装任何插件即可访问URL。我可以从文件中手动安装加载项。

此外,当我想添加个人资料时,出现以下错误

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'

解决方法

好吧,既然还没有人回答,我将分享如何解决这个问题,以防您想安装扩展程序。

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

第一种方法

我只是在弄乱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并直接使用selenium,但是Capybara也有一些很酷的方法。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...