class 未发现硒和 java

class not found with selenium and java

我正在尝试学习 selenium webdriver,但是 运行遇到了一个问题。

我做了一个非常小的测试,看起来像这样:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Flows {

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", "/Users/mn/Downloads/belikewater/chromedriver");
        WebDriver driver = new ChromeDriver();

        String baseUrl = "http://anywhere.c3a.dk/";


        driver.get(baseUrl);

        System.out.println(driver.getTitle());

        driver.close();

    }
}

我正在使用 gradle,它看起来像这样:

group 'com.cetrea'
version '1.0-SNAPSHOT'

apply plugin: 'java'

group = 'com.cetrea'
version = '1.0.0'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile('org.seleniumhq.selenium:selenium-java:2.41.0')
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

jar {
    manifest{
        attributes 'Main-Class': 'com.cetrea.flowtesting.Flows'
    }
}

我在

中遇到异常
"main" java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
    at java.lang.Class.getMethod0(Class.java:3018)
    at java.lang.Class.getMethod(Class.java:1784)
    at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.WebDriver
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 7 more

我正在使用 Mac OS,并尝试过 brew install chromedriver,但我找不到它的路径,因为我对 mac 也很陌生,所以我下载一个 zip 并将可执行的 chromedriver 复制到我的项目中。

也不记得如何获取完整的堆栈跟踪..

** 我一定是搞砸了。 运行 右键单击​​它,然后按 运行 flows.main() 给了我另一个错误,所以至少它现在找到了它需要的东西。但是我收到以下错误。

Starting ChromeDriver (v2.8.241036) on port 47434
[warn] kq_init: detected broken kqueue; not using.: Undefined error: 0
May 07, 2018 11:39:22 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Exception in thread "main" org.openqa.selenium.WebDriverException: java.net.ConnectException: Failed to connect to localhost/0:0:0:0:0:0:0:1:47434
Build info: version: '3.11.0', revision: 'e59cfb3', time: '2018-03-11T20:26:55.152Z'
System info: host: 'cetreas-MacBook-Pro.local', ip: 'fe80:0:0:0:c9e:2c67:1d27:4e0b%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.13.3', java.version: '1.8.0_161'
Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:92)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:545)
    at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:273)
    at com.cetrea.flows.Flows.main(Flows.java:15)
Caused by: java.net.ConnectException: Failed to connect to localhost/0:0:0:0:0:0:0:1:47434
    at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:240)
    at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:158)
    at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:256)
    at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:134)
    at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:113)
    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:125)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
    at okhttp3.RealCall.execute(RealCall.java:77)
    at org.openqa.selenium.remote.internal.OkHttpClient.execute(OkHttpClient.java:101)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:155)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    ... 3 more
Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at okhttp3.internal.platform.Platform.connectSocket(Platform.java:125)
    at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:238)
    ... 23 more

会不会是防火墙之类的东西阻止了它? mac借了半年了,还没完全适应

你好运行吗?

我怀疑这不是 Selenium 问题。看起来很可疑,您正在 运行 将您的应用程序设置为如下内容:java -jar your-jar-name.jar。但是您需要在 classpath 上提供所有必需的 jar。

一个简单的选择是将以下行添加到您的 gradle 文件

apply plugin: 'application'
mainClassName = "com.cetrea.flowtesting.Flows"

然后运行它作为gradle run

或者您可以要求 gradle 为您复制所有依赖 jar,然后在您的 class 路径中提及它们。

如果您下载了正确的驱动程序,应该运行成功。尽管您可能仍然需要一些更多的依赖项,如评论中提到的答案中所建议的那样。但至少 org.openqa.selenium.WebDriver 应该已经在你的 class 路径上。