Selenium Firefox WebDriver无法启动已安装的扩展程序

问题描述 投票:1回答:1

我有:

  1. Selenium Firefox WebDriver v.3.8.1
  2. 浏览器Firefox 43
  3. 带有firefox附加组件的XPI文件

我在浏览器中以两种方式运行扩展:jpm并使用java中的程序通过selenium firefox web-driver。

在第一种情况下,我运行命令jpm run,它创建了一个安装并运行扩展的新配置文件。在打开浏览器后立即自动启动扩展程序非常重要。

我需要获得相同的结果,但在selenium webdriver的帮助下。作为我的程序的结果,创建了一个安装了扩展的配置文件,但扩展的启动方式与执行jpm run命令时的启动方式不同。

请帮忙,了解可能出现的问题。

我的代码:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.FirefoxBinary;
import java.io.File;
import org.openqa.selenium.remote.DesiredCapabilities;

public class MyClass  extends Thread {
    private String baseUrl;
    public MyClass(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    public void run() {
        FirefoxProfile profile = new FirefoxProfile();
        profile.addExtension(new File("C:\\switcher.xpi"));
        profile.setPreference("[email protected]", "run");
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability(FirefoxDriver.PROFILE, profile);
        WebDriver driver = new FirefoxDriver(caps);
        driver.get(this.baseUrl);
    }

    public static void main( String[] args){
        System.setProperty("webdriver.firefox.marionette",
            "C:\\geckodriver.exe");
        Thread t1 = new MyClass("http://google.com");
        t1.start();
    }
}

附:我试图在硒webdriver的帮助下安装萤火虫 - 问题是一样的。

java selenium firefox firefox-addon selenium-firefoxdriver
1个回答
0
投票

我和你有同样的问题。所需的功能已弃用。要走的路是FirefoxOptions。

private void initializeFirefoxDriver() {
        //set the location to your gecho driver
        System.setProperty("webdriver.gecko.driver", USER_DIRECTORY.concat("\\drivers\\geckodriver.exe"));
        //instantiate the Firefox profile
        FirefoxProfile profile = new FirefoxProfile();
        //Adding the location to the extension you would like to use
        profile.addExtension(new File("Path_T0_Your_Saved_Extention\\try_xpath-1.3.4-an+fx.xpi"));
        //Setting the preference in which firefox will launch with.
        profile.setPreference("permissions.default.image", 2);
        //instantiating firefox options.
        FirefoxOptions options = new FirefoxOptions();
        //Passing the profile into the options object because nothing can ever be easy.
        options.setProfile(profile);
        //passing the options into the Firefox driver.
        webDriver = new FirefoxDriver(options);
        webDriver.manage().window().maximize();
        webDriverWait = new WebDriverWait(webDriver, GLOBAL_TIMEOUT);
    }

代码不完整,只是一个代码段,但这将启动您选择的扩展。 Firebug不再那么好,所以可能会看一下TruePath和Find Path扩展。 H

希望这有帮助。

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