有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

启动时使用selenium在Java中向firefox驱动程序添加扩展名(*.xpi文件)

我浏览了很多帖子,但没有一个能帮我让它工作

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

注意:如果我使用opiton“install addon from file..”将我的加载项(*.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");

硒依赖版本:(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

  • 当我运行上述代码时,Firefox borwser会打开,但插件没有安装

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

    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

共 (1) 个答案

  1. # 1 楼答案

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

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

    1.1。按住Windows键并按键盘上的R键,然后写入firefox.exe -p

    1.2。单击打开的窗口上的“创建配置文件”按钮,并使用向导创建新的配置文件(在我的示例中,我创建了MyCreatedProfile)

    enter image description here

    现在选择新创建的配置文件并单击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。快乐地享受它:) enter image description here

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

    您可以尝试在Github中推送源代码