大家好,我在这里的第一篇文章。我正在尝试在Java中以嵌入式模式设置Selenium和Browsermob代理(必须说我以前从未使用过Maven,Selenium或BrowserMob代理)。遵循BrowserMob Proxy的Github指令,我在Eclipse中设置了一个Maven-Project,并在pom.xml中添加了两个依赖项。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ba</groupId>
<artifactId>com.ba.project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.14.0</version>
</dependency>
<dependency>
<groupId>net.lightbody.bmp</groupId>
<artifactId>browsermob-core</artifactId>
<version>2.1.5</version>
<scope>test</scope>
</dependency>
</dependencies>
为了测试它是否有效...
public class Test {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "...");
WebDriver driver = new FirefoxDriver();
BrowserMobProxy proxy = new BrowserMobProxyServer();
proxy.start(0);
}}
...我得到this errordialog 然后是这个异常。
Exception in thread "main" java.lang.NoClassDefFoundError: net/lightbody/bmp/BrowserMobProxy
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.privateGetMethodRecursive(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException:
net.lightbody.bmp.BrowserMobProxy
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 7 more
当我从main方法中删除最后一行时,它可以正常工作。 有人知道我在这里做错什么吗?预先感谢。
答案 0 :(得分:1)
您可以替换
<dependency>
<groupId>net.lightbody.bmp</groupId>
<artifactId>browsermob-core</artifactId>
<version>2.1.5</version>
<scope>test</scope>
</dependency>
与
<dependency>
<groupId>net.lightbody.bmp</groupId>
<artifactId>browsermob-core</artifactId>
<version>2.1.5</version>
</dependency>
尝试一下?可能是POM文件的依赖关系解决问题。