我无法在我的Selenium项目中设置BrowserMob Proxy。如BrowserMob Proxy github页面中所述,我在代码中导入了它:
public class DriverBase {
public WebDriver driver;
public BrowserMobProxy proxy;
@Before
public void setUp() {
proxy = new BrowserMobProxyServer();
proxy.start(0);
Proxy sproxy = ClientUtil.createSeleniumProxy(proxy);
DesiredCapabilities caps=new DesiredCapabilities();
caps.setCapability(CapabilityType.PROXY, sproxy);
driver = new ChromeDriver(caps);
我从browsermob-core中排除了slf 4j传递依赖项,并添加了slf 4j-jdk 14,以便服务器可以启动(最初它不能启动)。
<dependency>
<groupId>net.lightbody.bmp</groupId>
<artifactId>browsermob-core</artifactId>
<version>2.1.5</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.8.0-beta0</version>
</dependency>
我还手动添加了com.google.guava19.0,因为服务器没有从最新的guava版本开始。
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
在我的测试类中,我只启动浏览器:
public class TrafficTest extends DriverBase{
@Test
public void testTraffic() {
driver.navigate().to("http://liveexpert.ru");
}
浏览器启动,但无法打开网站或访问Internet。原始的browsermob-core maven安装程序甚至无法启动服务器或浏览器。当前的安装程序抛出错误:
java.lang.NoSuchMethodError: com.google.common.net.HostAndPort.getHost()Ljava/lang/String;
以下是完整日志
Nov 10, 2017 2:50:17 PM org.littleshoot.proxy.impl.DefaultHttpProxyServer start
INFO: Starting proxy at address: 0.0.0.0/0.0.0.0:0
Nov 10, 2017 2:50:17 PM org.littleshoot.proxy.impl.DefaultHttpProxyServer doStart
INFO: Proxy listening with TCP transport
Nov 10, 2017 2:50:17 PM org.littleshoot.proxy.impl.DefaultHttpProxyServer doStart
INFO: Proxy started at address: /0:0:0:0:0:0:0:0:45981
Starting ChromeDriver 2.33.506092 (733a02544d189eeb751fe0d7ddca79a0ee28cce4) on port 10442
Only local connections are allowed.
Nov 10, 2017 2:50:18 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
Nov 10, 2017 2:50:19 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Nov 10, 2017 2:50:19 PM org.littleshoot.proxy.impl.ClientToProxyConnection exceptionCaught
SEVERE: (AWAITING_INITIAL) [id: 0x62631078, L:/127.0.1.1:45981 - R:/127.0.0.1:33244]: Caught an exception on ClientToProxyConnection
java.lang.NoSuchMethodError: com.google.common.net.HostAndPort.getHost()Ljava/lang/String;
是否有任何解决方法来设置驱动程序以访问互联网?
2条答案
按热度按时间kgsdhlau1#
现在的解决方案是使用guava 22.0。
Guava的版本18,19导致问题:
为什么是explained here?
最新版本的guava 23.x解决了这个问题,但反过来又引发了另一个问题:
2nbm6dog2#
我找不到任何依赖于Guava 22的selenium-java jar(在mavenrepo中),版本3.5.0应该,但它不在仓库中。所以我降级到3.4.0,这个组合对我有用:
唯一不匹配的是browsermob-core自然依赖于Guava 22而不是21,但对于我所需要的(日志请求)似乎可以工作