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

时间:2017-12-30 16:45:25

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

我有:

  1. Selenium Firefox WebDriver v.3.8.1
  2. 浏览器Firefox 43
  3. 带有firefox附加组件的XPI文件
  4. 我在浏览器中以两种方式运行扩展: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("extensions.@switcher.sdk.load.command", "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();
        }
    }
    

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

1 个答案:

答案 0 :(得分: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扩展。高

希望这会有所帮助。

相关问题