gpt4 book ai didi

java - Selenium Firefox WebDriver 不启动已安装的扩展

转载 作者:太空宇宙 更新时间:2023-11-04 10:49:07 24 4
gpt4 key购买 nike

我有:

  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 - 问题是一样的。

最佳答案

我也遇到了和你一样的问题。所需的功能已被弃用。正确的方法是 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 扩展。

希望这有帮助。

关于java - Selenium Firefox WebDriver 不启动已安装的扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48035913/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com