【问题标题】:How to install extension permanently in geckodriver如何在 geckodriver 中永久安装扩展
【发布时间】:2019-07-12 06:57:07
【问题描述】:

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

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

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

【问题讨论】:

    标签: selenium selenium-webdriver firefox firefox-addon geckodriver


    【解决方案1】:

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

    【讨论】:

    • 谢谢。可以一步一步给我吗?我在 Firefox 开发者版中安装了扩展。 Firefox 开发者版在我的设备中的这条路径中:/home/xx/Documents/ff_extension/firefox/firefox.exe。在哪里可以找到 Firefox 开发者版的默认配置文件的路径?我什么也没找到。
    • 我测试过了。我收到此错误:NameError: name 'FirefoxProfile' is not defined。我也尝试添加这一行但没有解决问题:from selenium.webdriver.firefox.firefox_binary import FirefoxProfile
    • 我真的不知道开发者版的资料保存在哪里
    • 看看这个link
    【解决方案2】:

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


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

    首先,从https://addons.mozilla.org 下载所需的扩展名(XPI 文件)。

    然后,在您的代码中...创建一个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) 
    

    【讨论】:

    【解决方案3】:

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

    • 您需要按照Creating a new Firefox profile on Windows 的说明手动创建一个新的Firefox 配置文件(例如FirefoxExtensionProfile)。
    • 手动打开 Firefox 浏览 会话并调用 URL https://addons.mozilla.org/en-US/firefox/
    • 搜索框 中搜索扩展名,例如HTTPS 无处不在
    • 点击搜索结果并安装/启用(以防之前安装和当前禁用)扩展。
    • 现在您可以使用以下 Java 解决方案打开 Firefox 配置文件 FirefoxExtensionProfile 包含扩展 HTTPS Everywhere

      • 代码块:

        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");
            }
        }
        
      • 浏览器快照:


    参考

    您可以在以下位置找到一些相关讨论:

    【讨论】:

      【解决方案4】:

      我发现这对我来说很有趣:

      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/

      【讨论】:

        猜你喜欢
        • 2019-08-10
        • 1970-01-01
        • 2014-08-30
        • 2020-11-24
        • 2017-05-02
        • 2021-08-23
        • 2020-11-25
        相关资源
        最近更新 更多