使用ChromeDriver设置browsermob代理

Jul*_*ian 5 java proxy selenium-chromedriver selenium-webdriver browsermob

我试图设置browsermob在我的硒项目中工作。我一直在寻找一种使用ChromeOptions设置代理的方法,但是所有消息来源都告诉我在所有其他示例中都使用ChromeOptions,然后在实例化新的ChromeDriver实例之前将其转换为DesiredCapabilities。

这是我的代码:

ChromeOptions options = new ChromeOptions();
// Setting some chrome features here

ProxyServer proxyServer = new ProxyServer(4444);
proxyServer.start();

Proxy proxy = proxyServer.seleniumProxy();

DesiredCapabilities capabilities = DesiredCapabilities.chrome();

capabilities.setCapability(ChromeOptions.CAPABILITY, options);
capabilities.setCapability(CapabilityType.PROXY, proxy);

WebDriver driver = new ChromeDriver(capabilities); // Error happens here
Run Code Online (Sandbox Code Playgroud)

我正在使用来自Maven存储库的Webdriver版本2.44。这是我得到的错误:

java.lang.IllegalAccessError: tried to access field com.google.gson.JsonNull.INSTANCE from class org.openqa.selenium.remote.BeanToJsonConverter
Run Code Online (Sandbox Code Playgroud)

是否有人知道将代理连接到chromedriver的原因或任何其他解决方案?

Jas*_*ger 2

如果您使用旧版本的 browsermob-proxy,Selenium 的依赖项和 BMP 之间可能会存在一些冲突。我建议使用最新的 Selenium +从 master构建最新的 BrowserMob 代理。

一旦您拥有最新版本,您应该能够以“通常”方式使用 Chrome + BMP:

        BrowserMobProxy proxy = new BrowserMobProxyServer();
        proxy.start(); // can specify a port here if you like

        // get the selenium proxy object
        Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);

        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);

        // if chromedriver isn't on your system path, you'll need to set this system property
        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
        WebDriver driver = new ChromeDriver(capabilities);

        driver.get("https://www.google.com/");
Run Code Online (Sandbox Code Playgroud)