简体   繁体   中英

The Selenium Firefox WebDriver does not start the installed extension

I have:

  1. Selenium Firefox WebDriver v.3.8.1
  2. Browser Firefox 43
  3. XPI-file with firefox add-on

I ran the extension in the browser in two ways: jpm and using the program on the java through selenium firefox web-driver.

In the first case, I run command jpm run , which creates a new profile with the extension installed and running. It is important that the extension is automatically launched immediately after opening the browser.

I need to achieve the same result, but with the help of the selenium webdriver. As a result of my program, a profile is created with the extension installed, but the extension does not start the same way as when executing the jpm run command.

Help, please, understand what can be the problem.

My code:

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();
    }
}

PS I tried to install the firebug with the help of a selenium webdriver - the problem is the same.

I had the same issue as you. Desired capabilities are deprecated. The way to go is 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);
    }

The code is not complete and just a snippet, but that will start an extension of your choice. Firebug is no more as well so maybe look at TruePath and Find Path extensions. H

Hope this helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM