Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium 如何在geckodriver中永久安装扩展_Selenium_Selenium Webdriver_Firefox_Firefox Addon_Geckodriver - Fatal编程技术网

Selenium 如何在geckodriver中永久安装扩展

Selenium 如何在geckodriver中永久安装扩展,selenium,selenium-webdriver,firefox,firefox-addon,geckodriver,Selenium,Selenium Webdriver,Firefox,Firefox Addon,Geckodriver,我需要使用扩展测试Firefox。我想自动化测试并访问几个网站 我安装了Selenium,它在geckodriver中打开。然而,扩展并不存在。我可以从about:debug手动安装它,但问题是我希望Selenium测试在扩展已经存在时启动gecko驱动程序。如何做到这一点?如何将扩展永久安装在geckodriver中,这样当我从selenium启动geckodriver时它就在那里了 编辑: 我还尝试从Firefox扩展网站安装扩展(将其添加到浏览器)。它被添加了,但是一旦我关闭gecko窗口

我需要使用扩展测试Firefox。我想自动化测试并访问几个网站

我安装了Selenium,它在
geckodriver
中打开。然而,扩展并不存在。我可以从
about:debug
手动安装它,但问题是我希望Selenium测试在扩展已经存在时启动gecko驱动程序。如何做到这一点?如何将扩展永久安装在
geckodriver
中,这样当我从selenium启动
geckodriver
时它就在那里了

编辑:
我还尝试从Firefox扩展网站安装扩展(将其添加到浏览器)。它被添加了,但是一旦我关闭gecko窗口,扩展就会在下一次运行时消失。如何永久安装?

您需要通过指定firefox的配置文件路径,使用现有配置文件启动geckdriver

对于python,您可以通过以下方式完成:

profile = FirefoxProfile('/home/student/.mozilla/firefox/gwi6uqpe.Default') // change this path
browser = webdriver.Firefox(firefox_profile=profile)
对于C#您可以这样做:

string path = @"C:\Users\username\AppData\Local\Mozilla\Firefox\Profiles\myi5go1k.default";
FirefoxProfile ffprofile = new FirefoxProfile(path);
Driver = new FirefoxDriver(ffprofile);
注意:OP没有指定语言,所以这个答案是针对Python的。其他SeleniumWebDriver语言绑定具有类似的机制来创建概要文件和添加扩展


您可以在每次实例化驱动程序时安装扩展

首先,从以下位置下载所需的扩展名(XPI文件):

然后,在代码中。。。创建
FirefoxProfile()
并使用
add_extension()
方法添加扩展。然后可以使用该配置文件实例化驱动程序

例如,这将使用新创建的配置文件启动Firefox,其中包含“HTTPS Everywhere”扩展:

from selenium import webdriver

profile = webdriver.FirefoxProfile() 
profile.add_extension(extension='https_everywhere-2019.1.31-an+fx.xpi')
driver = webdriver.Firefox(firefox_profile=profile) 

您可以在特定的Firefox配置文件中永久安装扩展/插件并使用它。要实现这一目标,您需要遵循以下提到的步骤:

  • 您需要按照中的说明手动创建新的Firefox配置文件(例如,FirefoxExtensionProfile
  • 手动打开Firefox浏览会话并调用url
    https://addons.mozilla.org/en-US/firefox/
  • 在搜索框中搜索扩展名,例如HTTPS Everywhere
  • 单击搜索结果并安装/启用(如果以前已安装且当前已禁用)扩展
  • 现在,您可以使用以下Java解决方案打开Firefox配置文件FirefoxExtensionProfile,其中包含扩展HTTPS

    • 代码块:

      package A_MozillaFirefox;
      
      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.firefox.FirefoxDriver;
      import org.openqa.selenium.firefox.FirefoxOptions;
      import org.openqa.selenium.firefox.FirefoxProfile;
      import org.openqa.selenium.firefox.ProfilesIni;
      
      public class A_FirefoxProfile_dc_opt {
      
          public static void main(String[] args) {
      
              System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
              ProfilesIni profile = new ProfilesIni();
              FirefoxProfile testprofile = profile.getProfile("FirefoxExtensionProfile");
              FirefoxOptions opt = new FirefoxOptions();
              opt.setProfile(testprofile);
              WebDriver driver =  new FirefoxDriver(opt);
              driver.get("https://www.google.com");
          }
      }
      
    • 浏览器快照:


参考文献 您可以在以下内容中找到一些相关讨论:

  • [Python]
  • [Python]

谢谢。你能一步一步地告诉我吗?我已经在Firefox开发者版中安装了扩展。Firefox开发者版在我的设备中位于以下路径:
/home/xx/Documents/ff_extension/Firefox/Firefox.exe
。在哪里可以找到Firefox开发者版的默认配置文件路径?我什么也没找到,我测试过了。我收到以下错误:
name错误:未定义名称“FirefoxProfile”
。我还试图添加这一行,但没有解决问题:
来自selenium.webdriver.firefox.firefox\u二进制导入FirefoxProfile
我真的不知道Developer edition的配置文件保存在哪里