【问题标题】:Add extension(*.xpi file) to firefox driver in Java using selenium when launched启动时使用 selenium 将扩展名(*.xpi 文件)添加到 Java 中的 firefox 驱动程序
【发布时间】:2021-11-06 18:59:34
【问题描述】:

我浏览了许多帖子,但没有一个可以帮助我让它发挥作用。

我想使用 selenium 和 Java 程序安装我的 firefox 插件,即在启动 firefox 时安装插件。

注意:如果我使用设置中的选项“从文件安装插件..”选项手动将插件(*.xpi 文件)安装到 Firefox 浏览器,它会正确安装。

Java 代码:

        System.setProperty("webdriver.gecko.driver", "geckodriver-v0.24.0-win64\\geckodriver.exe");
        FirefoxOptions firefoxOptions = new FirefoxOptions();
        FirefoxProfile profile = new FirefoxProfile();
        profile.addExtension(new File("my_webext.xpi"));        
        firefoxOptions.setProfile(profile);
        WebDriver firefoxDriver = new FirefoxDriver(firefoxOptions);
        firefoxDriver.get("http://www.google.com");

Selenium 依赖版本:(4.0.0-rc-1/4.0.0-alpha-2/3.141.59)

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.0.0-alpha-2</version>
        </dependency>

Gecko 驱动程序版本:0.24.0 或 0.29.1

Firefox 版本:92.0(64 位)

IDE:STS

操作系统:Windows 10

  • 当我运行上面的代码时,火狐浏览器打开了,但是插件没有安装。

  • 我在控制台上看到以下消息:

    1631259829322   mozrunner::runner   INFO    Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "-marionette" "-foreground" "-no-remote" "-profile" "C:\\Users\\ab\\AppData\\Local\\Temp\\rust_mozprofile.CSQe58I2OCxk"
    1631259830585   Marionette  INFO    Marionette enabled
    JavaScript error: resource://gre/modules/XULStore.jsm, line 66: Error: Can't find profile directory.
    console.warn: SearchSettings: "get: No settings file exists, new profile?" (new NotFoundError("Could not open the file at C:\\Users\\ab\\AppData\\Local\\Temp\\rust_mozprofile.CSQe58I2OCxk\\search.json.mozlz4", (void 0)))
    1631259834207   Marionette  INFO    Listening on port 59932
    1631259834612   RemoteAgent WARN    TLS certificate errors will be ignored for this session
    Sep 10, 2021 1:13:54 PM org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Detected dialect: W3C

【问题讨论】:

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


    【解决方案1】:

    您应该首先创建一个 Firefox 配置文件,然后将其嵌入到 selenium 代码中。您可以使用默认配置文件,但由于以前的现金和不必要的插件,您可能会在第一次运行时遇到“Java Heap”错误。所以第一步是:

    1.创建新的 Firefox 配置文件:

    1.1。按住 Windows 键并按下键盘上的 R 并输入firefox.exe -p

    1.2。单击打开的窗口上的创建配置文件按钮并使用向导创建一个新配置文件(在我的情况下,我创建了 MyCreatedProfile)

    现在选择您新创建的个人资料并点击Start Firefox 按钮

    2.安装插件:

    在 Firefox 窗口中搜索你想要的插件并安装插件,我搜索了discard-tab 并安装了它。已安装的插件现在位于您创建的配置文件中。

    3.在 selenium webDriver 中编写代码:

    此代码应使用已定义的配置文件打开 Firefox(其中安装了插件)

    System.setProperty("webdriver.gecko.driver", "c:/geckodriver.exe");
    
    ProfilesIni profilesIni = new ProfilesIni();
    FirefoxProfile profile = profilesIni.getProfile("MyCreatedProfile");
    FirefoxOptions firefoxOptions= new FirefoxOptions();
    firefoxOptions.setProfile(profile);
    WebDriver driver =  new FirefoxDriver(firefoxOptions);
    try {
         driver.get("https://google.com/");
         Thread.sleep(60000);
     } catch (Exception e) {
         e.printStackTrace();
     } finally {
         driver.quit();
     }
    

    4.愉快地享受它:)

    请注意,在此方法中,您无需在每次创建新的 Firefox 驱动程序时都安装每个扩展程序。当您需要多个扩展时,它特别有用。

    你可以试试Github推送的源码

    【讨论】:

    • 谢谢。试过了,没用。这是代码: FirefoxOptions firefoxOptions = new FirefoxOptions(); ProfilesIni profileIni = new ProfilesIni(); FirefoxProfile profile = profileIni.getProfile("MyProfileName"); profile.addExtension(new File("my_addon.xpi")); firefoxOptions.setProfile(profile); WebDriver firefoxDriver = new FirefoxDriver(firefoxOptions); firefoxDriver.get("http://www.google.com");
    • 同样使用上面的代码,我确实在控制台上看到了警告:无法在 /search.json.mozlz4 打开文件
    • 我试图在答案中解释更多。希望对你有帮助
    • 谢谢@BzH。对不起,你能解释一下吗?我看到 API 有一些不同,您使用的是什么版本的 selenium 库?当你说“当你使用这个解决方案时,你已经在浏览器上永久拥有了扩展”是什么意思?我的目标是在以编程方式启动 FF 时安装我的扩展。很抱歉有这么多问题和困惑
    • 我尝试编写代码并对其进行测试...代码对我来说工作正常...检查编辑的答案,一步一步地做,如果问题存在,请毫不犹豫地提出任何其他问题。祝你好运
    猜你喜欢
    • 2019-12-27
    • 2016-06-21
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 2015-08-09
    • 2014-01-03
    • 2013-04-25
    相关资源
    最近更新 更多