如何在geckodriver中永久安装扩展程序

问题描述 投票:2回答:3

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

我安装了Selenium,它在geckodriver打开。但是,扩展不存在。我可以从about:debugging手动安装它,但问题是我希望Selenium测试启动gecko驱动程序,而扩展已经存在。这该怎么做?如何在geckodriver中永久安装扩展,以便当我从硒中启动geckodriver时它就在那里?

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

selenium firefox selenium-webdriver geckodriver selenium-firefoxdriver
3个回答
1
投票

注意:OP没有指定语言,因此这个答案适用于Python。其他Selenium WebDriver语言绑定具有类似的机制,用于创建配置文件和添加扩展。


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

首先,下载您想要的扩展名(XPI文件):https://addons.mozilla.org

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

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

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) 

1
投票

您需要通过指定firefox的配置文件路径来启动具有现有配置文件的geckodriver

对于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);

1
投票

您可以在特定的Firefox配置文件中永久安装扩展程序并使用它。为此,您需要按照以下步骤操作:

  • 您需要按照Creating a new Firefox profile on Windows上的说明手动创建一个新的Firefox配置文件(例如FirefoxExtensionProfile)。
  • 手动打开Firefox浏览会话并调用url https://addons.mozilla.org/en-US/firefox/
  • 在搜索框中搜索扩展名,例如HTTPS无处不在。
  • 单击搜索结果并安装/启用(包括以前安装且当前已禁用)扩展。
  • 现在,您可以使用以下Java解决方案打开包含HTTPS Everywhere扩展名的Firefox Profile FirefoxExtensionProfile 代码块: 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"); } } 浏览器快照:

Extension_HTTPS Everywhere

© www.soinside.com 2019 - 2024. All rights reserved.