0

我正在尝试GeckoDriverselenium-java 3.11. 火狐版本是59. 一切正常,并且能够调用 firefox 驱动程序。唯一的挑战是我需要添加DesiredCapabilities到我的 Firefox 实例中。

最新硒版本中的旧方法WebDriver driver = new FirefoxDriver(capabilities);似乎已被弃用,并且无法找出指定功能的新方法。

这是代码:

public static WebDriver createDriver(String userAgentKey, String PROXY) {


     File modifyHeaders = null;
     try {
     modifyHeaders =
     ResourceUtils.getFile("classpath:modify_headers-0.7.1.1-fx.xpi");  //am adding the extension using DesiredCapabilities
     } catch (FileNotFoundException e1) {

     e1.printStackTrace();
     }

    Proxy proxy = new Proxy();
    proxy.setHttpProxy(PROXY).setFtpProxy(PROXY).setSslProxy(PROXY);



    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("javascript.enabled", true);
    profile.setPreference("brower.link.open_newwindow", 1);


    profile.setPreference("modifyheaders.config.active", true);
    profile.setPreference("modifyheaders.config.alwaysOn", true);
    profile.setPreference("modifyheaders.headers.count", 1);
    profile.setPreference("modifyheaders.headers.action0", "Add");
    profile.setPreference("modifyheaders.headers.name0", "Proxy-Authorization");
    profile.setPreference("modifyheaders.headers.value0", "Basic dnNodWtsYUBleHBlZGlhLmNvbTpxZWxsYWZraw==");
    profile.setPreference("modifyheaders.headers.enabled0", true);

    profile.addExtension(modifyHeaders);

    DesiredCapabilities cap = DesiredCapabilities.firefox();
    cap.setCapability(FirefoxDriver.PROFILE, profile);
    cap.setCapability(CapabilityType.PROXY, proxy);
    System.setProperty("webdriver.gecko.driver","/Users/vshukla/Music/geckodriver");

    WebDriver driver = new FirefoxDriver(cap);  //this is deprecated


    return driver;

如何使用 DesiredCapabilities 将扩展添加到实例?实现这一目标的其他选择是什么?

4

2 回答 2

0

你快到了。您需要使用 MutableCapabilities 类中的方法merge()DesiredCapabilities类型的对象合并FirefoxOptions类型的对象中,并通过传递FirefoxOptions对象来启动WebDriverWebClient实例,如下所示:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.remote.DesiredCapabilities;

System.setProperty("webdriver.gecko.driver","/Users/vshukla/Music/geckodriver");
DesiredCapabilities cap = DesiredCapabilities.firefox();
cap.setCapability(FirefoxDriver.PROFILE, profile);
cap.setCapability(CapabilityType.PROXY, proxy);
FirefoxOptions opt = new FirefoxOptions();
opt.merge(cap);
WebDriver driver = new FirefoxDriver(opt);

PS:作为参考,您可以查看mutablecapabilities标签中的讨论

于 2018-04-19T09:49:08.313 回答
0

使用火狐选项:

    FirefoxOptions options = new FirefoxOptions();
    options.setCapability(FirefoxDriver.PROFILE, profile);
    options.setCapability(CapabilityType.PROXY, proxy);
于 2018-04-19T09:23:55.657 回答