我无法在我的Selenium项目中设置BrowserMob代理。就像described in BrowserMob Proxy github page我在我的代码中导入它一样:
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,因为服务器没有以最新的番石榴版本开头。
<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");
}
浏览器启动,但不会打开网站或访问互联网。原始的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;
是否有解决方法来设置驱动程序以访问互联网?
答案 0 :(得分:1)
现在解决方案是使用guava 22.0。
番石榴版本18,19引起了问题:
java.lang.NoSuchMethodError: com.google.common.net.HostAndPort.getHost()
为什么explained here。
最新版本的guava 23.x解决了这个问题,但反过来又引发了另一个问题:
java.lang.IllegalAccessError: tried to access method com.google.common.util.concurrent.SimpleTimeLimiter.<init>(Ljava/util/concurrent/ExecutorService;)V from class org.openqa.selenium.net.UrlChecker