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

3
我需要使用一个扩展来测试Firefox。我想自动化测试并访问多个网站。
我安装了Selenium,它在geckodriver中打开。但是,该扩展没有出现。我可以从"about:debugging"手动安装它,但问题是,我希望Selenium测试在扩展已经存在的情况下启动gecko driver。如何做到这一点?如何永久地将扩展安装在geckodriver中,以便在我从selenium中启动geckodriver时扩展已经存在?
编辑:我还尝试从Firefox扩展网站安装扩展(将其添加到浏览器中)。它被添加了,但一旦我关闭了gecko窗口,在下一次运行中它就消失了。如何永久安装它?
4个回答

5

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


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

首先,从https://addons.mozilla.org下载所需的扩展程序(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) 

我们正在尝试在Nightwatch.js中实现类似于这样的操作。 https://stackoverflow.com/questions/69565983/how-to-configure-nightwatch-js-with-geckodriver-to-install-metamask-on-browser-s这里有人知道如何实现吗? - glo

4
您需要通过指定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);

谢谢。你能给我一步一步的指导吗?我已经在Firefox开发者版中安装了扩展程序。Firefox开发者版在我的设备上的路径是:/home/xx/Documents/ff_extension/firefox/firefox.exe。我在哪里可以找到Firefox开发者版的默认配置文件路径?我找不到任何信息。 - user9371654
我进行了测试,但是出现了以下错误:NameError: name 'FirefoxProfile' is not defined。我还尝试添加了这行代码,但问题仍未解决:from selenium.webdriver.firefox.firefox_binary import FirefoxProfile - user9371654
我真的不知道开发者版将其配置文件保存在哪里。 - Oussail
请查看此链接 - Oussail

3
我发现这对我有用:
from selenium import webdriver

driver_path = r"G:\Libs\geckoDriver\firefox\geckodriver.exe"
driver = webdriver.Firefox(executable_path=driver_path)

path = r"G:\Libs\ext\uBlock0_1.38.7b5.firefox.signed.xpi"
driver.install_addon(path, temporary=True)

driver.profile = webdriver.FirefoxProfile()
driver.profile.add_extension(path)
driver.profile.set_preference("security.fileuri.strict_origin_policy", False)
driver.profile.update_preferences()`enter code here`

参考资料:

[Python]https://cyruslab.net/2020/08/26/python-adding-extension-to-geckodriver-with-selenium/


2
你可以在特定的Firefox配置文件中永久安装扩展/插件并使用它。要实现这一点,您需要按照以下步骤操作:
  • You need to create a new Firefox Profile manually (e.g. FirefoxExtensionProfile) following the instructions at Creating a new Firefox profile on Windows.

  • Open a Firefox Browsing session manually and invoke the url https://addons.mozilla.org/en-US/firefox/

  • In the Search Box search for an extension e.g. HTTPS Everywhere.

  • Click on the search result and install / enable (incase previously installed and currently disabled) the extension.

  • Now you can use the following Java solution to open the Firefox Profile FirefoxExtensionProfile containing the extension HTTPS Everywhere

  • Code Block:

    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");
     }
    }
    
  • Browser Snapshot:

Extension_HTTPS Everywhere的翻译是“HTTPS无处不在扩展程序”。

参考资料

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接