【问题标题】:Setting up browsermob proxy with ChromeDriver使用 ChromeDriver 设置 browsermob 代理
【发布时间】:2015-07-09 11:42:30
【问题描述】:

我正在尝试设置 browsermob 以在我的 selenium 项目中工作。我一直在寻找一种使用 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

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

java.lang.IllegalAccessError: tried to access field com.google.gson.JsonNull.INSTANCE from class org.openqa.selenium.remote.BeanToJsonConverter

有人知道将代理连接到 chromedriver 的原因或任何替代解决方案吗?

【问题讨论】:

标签: java selenium-webdriver proxy selenium-chromedriver browsermob


【解决方案1】:

Browsermob-Proxy 是一个可靠的解决方案,但是在使用远程网格机器时,Browsermob-proxy 并没有真正的帮助。或者,我发现这是我的设置的工作解决方案。

希望它对具有类似设置的人有用。

  1. 将 ModHeader 扩展添加到 chrome 浏览器

如何下​​载 Modheader?链接

ChromeOptions options = new ChromeOptions();
options.addExtensions(new File(C://Downloads//modheader//modheader.crx));

// Set the Desired capabilities 
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);

// Instantiate the chrome driver with capabilities
WebDriver driver = new RemoteWebDriver(new URL(YOUR_HUB_URL), options);
  1. 转到浏览器扩展并捕获 ModHeader 的本地存储上下文 ID

  1. 导航到 ModHeader 的 URL 以设置本地存储上下文

.

// set the context on the extension so the localStorage can be accessed
driver.get("chrome-extension://idgpnmonknjnojddfkpgkljpfnnfcklj/_generated_background_page.html");

Where `idgpnmonknjnojddfkpgkljpfnnfcklj` is the value captured from the Step# 2
  1. 现在使用Javascript将标头添加到请求中

.

   ((Javascript)driver).executeScript(
         "localStorage.setItem('profiles', JSON.stringify([{  title: 'Selenium', hideComment: true, appendMode: '', 
             headers: [                        
               {enabled: true, name: 'token-1', value: 'value-1', comment: ''},
               {enabled: true, name: 'token-2', value: 'value-2', comment: ''}
             ],                          
             respHeaders: [],
             filters: []
          }]));");

其中token-1value-1token-2value-2 是要添加的请求标头和值。

  1. 现在导航到所需的网络应用程序。

    driver.get("your-desired-website");

【讨论】:

    【解决方案2】:

    ChromeDriver 不直接支持代理上限。但它确实 支持将命令行参数传递给 chrome 进程。并设置 http 代理是 chrome 命令行开关之一。可以设置 如下:

    DesiredCapabilities caps = DesiredCapabilities.chrome();    
    ArrayList<String> switches = new ArrayList<String>();    
    switches.add("--proxy-server=localhost:8080");    
    caps.setCapability("chrome.switches", switches);    
    webDriver = new ChromeDriver(caps);    
    

    【讨论】:

    • 我用 selenium 2.52 和 bmp 2.1.0-beta4 测试这种方式,完美。 jekh 提出的解决方案仅适用于 firefox 驱动程序!谢谢你,伙计!
    【解决方案3】:

    如果您使用的是旧版本的 browsermob-proxy,则 Selenium 的依赖项和 BMP 的依赖项之间可能存在一些冲突。我建议使用最新的 Selenium + 构建来自 master 的 latest BrowserMob Proxy

    获得最新版本后,您应该能够以“通常”的方式使用 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/");
    

    【讨论】:

      最近更新 更多