【问题标题】:The Selenium Firefox WebDriver does not start the installed extensionSelenium Firefox WebDriver 不启动已安装的扩展
【发布时间】:2018-06-10 16:19:24
【问题描述】:

我有:

  1. Selenium Firefox WebDriver v.3.8.1
  2. 浏览器 Firefox 43
  3. 带有 Firefox 插件的 XPI 文件

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

在第一种情况下,我运行命令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();
    }
}

附:我尝试在 selenium webdriver 的帮助下安装 firebug - 问题是一样的。

【问题讨论】:

  • 你有什么错误信息吗?
  • @RatmirAsanov 不,没有错误。浏览器以安装的扩展程序启动,不会发出 java 错误和警告。

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


【解决方案1】:

我和你有同样的问题。不推荐使用所需的功能。要走的路是 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);
    }

代码不完整,只是一个 sn-p,但这将启动您选择的扩展。 Firebug 已经不复存在了,所以也许看看 TruePath 和 Find Path 扩展。 H

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2016-01-16
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 2014-07-03
    • 2016-05-03
    • 1970-01-01
    • 2013-09-30
    • 2015-02-28
    相关资源
    最近更新 更多