Java 使用ChromeDriver设置browsermob代理
我正在尝试设置browsermob以在我的selenium项目中工作。我一直在寻找一种使用ChromeOptions设置代理的方法,但所有来源都告诉我,在实例化新的ChromeDriver实例之前,要将ChromeOptions用于其他所有功能,然后将其转换为所需的功能 这是我的代码:Java 使用ChromeDriver设置browsermob代理,java,selenium-webdriver,proxy,selenium-chromedriver,browsermob,Java,Selenium Webdriver,Proxy,Selenium Chromedriver,Browsermob,我正在尝试设置browsermob以在我的selenium项目中工作。我一直在寻找一种使用ChromeOptions设置代理的方法,但所有来源都告诉我,在实例化新的ChromeDriver实例之前,要将ChromeOptions用于其他所有功能,然后将其转换为所需的功能 这是我的代码: ChromeOptions options = new ChromeOptions(); // Setting some chrome features here ProxyServer proxyServer
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的原因或其他解决方案吗?如果您使用的是旧版本的browsermob代理,Selenium的依赖项和BMP之间可能存在一些冲突。我建议使用master最新的Selenium+构建 拥有最新版本后,您应该能够以“常规”方式使用Chrome+BMP:
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);
DesiredCapabilities=DesiredCapabilities.chrome();
ArrayList开关=新的ArrayList();
add(“--proxy server=localhost:8080”);
caps.setCapability(“铬开关”,开关);
webDriver=新的ChromeDriver(caps);
Browsermob Proxy
是一个可靠的解决方案,但在使用远程网格计算机时,Browsermob Proxy并没有真正的帮助。或者,我发现这是我的设置的有效解决方案
希望它对具有类似设置的人有用
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);
Javascript
token-1
,value-1
,token-2
,value-2
是要添加的请求头和值
driver.get(“您想要的网站”)代码>
试试这个博客——我用selenium 2.52和bmp 2.1.0-beta4来测试,非常好。jekh提出的解决方案只适用于firefox驱动程序!谢谢你,伙计!
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);
// 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
((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: []
}]));");