一尘不染

无法为Selenium / WebDriver运行Java示例

selenium

在Java上使用Selenium时遇到问题。我正在尝试遵循此页面上的示例:http
:
//code.google.com/p/selenium/wiki/GettingStarted

我输入这个:

$ javac -cp . -cp ~/Downloads/selenium-2.20.0/selenium-java-2.20.0-srcs.jar -cp ~/Downloads/selenium-2.20.0/selenium-java-2.20.0.jar -g Example.java

得到这个:

alis-mac-pro:selenium ali$ java ExampleException in thread "main" java.lang.NoClassDefFoundError: Example (wrong name: org/openqa/selenium/example/Example)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

我将示例中的代码粘贴到Example.java中。

更新


irc.freenode.net上#java上的一个好心人告诉我将我的条目链接到类路径中。因此,代替:

$ javac -cp . -cp ~/Downloads/selenium-2.20.0/selenium-java-2.20.0-srcs.jar -cp ~/Downloads/selenium-2.20.0/selenium-java-2.20.0.jar -g Example.java

我用了:

javac -cp .:~/Downloads/selenium-2.20.0/selenium-java-2.20.0-srcs.jar:~/Downloads/selenium-2.20.0/selenium-java-2.20.0.jar -g Example.java

大!:-)但是现在出现新错误:

Example.java:3: cannot find symbol
symbol  : class By
location: package org.openqa.selenium
import org.openqa.selenium.By;
                          ^
Example.java:4: cannot find symbol
symbol  : class WebDriver
location: package org.openqa.selenium
import org.openqa.selenium.WebDriver;
                          ^
Example.java:5: cannot find symbol
symbol  : class WebElement
location: package org.openqa.selenium
import org.openqa.selenium.WebElement;
                          ^
Example.java:6: package org.openqa.selenium.htmlunit does not exist
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
                                   ^
Example.java:13: cannot find symbol
symbol  : class WebDriver
location: class org.openqa.selenium.example.Example
        WebDriver driver = new HtmlUnitDriver();
        ^
Example.java:13: cannot find symbol
symbol  : class HtmlUnitDriver
location: class org.openqa.selenium.example.Example
        WebDriver driver = new HtmlUnitDriver();
                               ^
Example.java:19: cannot find symbol
symbol  : class WebElement
location: class org.openqa.selenium.example.Example
        WebElement element = driver.findElement(By.name("q"));
        ^
Example.java:19: cannot find symbol
symbol  : variable By
location: class org.openqa.selenium.example.Example
        WebElement element = driver.findElement(By.name("q"));
                                                ^
8 errors

更新


我试过了:

$ jar tf ~/Downloads/selenium-2.20.0/selenium-java-2.20.0.jar

但是例如,我看到的是org.openqa.selenium.By类,因此似乎没有丢失任何软件包。

更新


好的!:-)我删除了包说明符。我执行了:

$ javac -classpath ~/Downloads/selenium-2.20.0/selenium-java-2.20.0.jar Example.java

这是评论者给我的,除了我向.jar添加了完整路径。然后我执行了:

$ java -cp .:~/Downloads/selenium-2.20.0/selenium-java-2.20.0.jar Example

这给了我:

Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.WebDriver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

更新


欢呼!胜利,感谢关先生。以下是有效的方法:

alis-mac-pro:selenium ali$ javac -cp ".:/Users/ali/Downloads/selenium-2.20.0/selenium-java-2.20.0.jar:/Users/ali/Downloads/selenium-2.20.0/selenium-java-2.20.0-srcs.jar:/Users/ali/Downloads/selenium-2.20.0/libs/*" Example.java
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
alis-mac-pro:selenium ali$ java -cp ".:/Users/ali/Downloads/selenium-2.20.0/selenium-java-2.20.0.jar:/Users/ali/Downloads/selenium-2.20.0/selenium-java-2.20.0-srcs.jar:/Users/ali/Downloads/selenium-2.20.0/libs/*" Example
Page title is: Cheese! - Google Search

顺便说一句,我添加了“ -cp”选项的最后一部分(对于“ libs / *”),因为这是我下载Selenium /
WebDriver内容时的HTMLUnit。


阅读 349

收藏
2020-06-26

共1个答案

一尘不染

像这样编译:

javac -classpath selenium-server-standalone-2.20.0.jar Example.java

除非将Java文件放在中org.openqa.selenium.example,否则必须删除包说明符。像这样运行编译后的文件:

java -cp .:selenium-server-standalone-2.20.0.jar Example

这会将当前目录链接到类路径。

2020-06-26