我无法在我的 Selenium 项目中设置 BrowserMob 代理。就像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 中排除了 slf4j 传递依赖并添加了 slf4j-jdk14,以便服务器可以启动(最初它不能)。
<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.guava 19.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;
是否有任何解决方法来设置驱动程序以访问互联网?