我无法在我的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,因为服务器不是以最新的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;
是否有任何解决方法来设置驱动程序以访问Internet?
最佳答案
到目前为止,解决方案是使用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
关于java - BrowserMob代理和Selenium的问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47223626/