简体   繁体   English

无法为 Selenium / WebDriver 运行 Java 示例

[英]Can't run Java example for Selenium / WebDriver

Having a problem getting going with Selenium on Java. I'm trying to follow the example on this page: http://code.google.com/p/selenium/wiki/GettingStarted在 Java 上使用 Selenium 时遇到问题。我正在尝试按照此页面上的示例进行操作: http://code.google.com/p/selenium/wiki/GettingStarted

I'm typing this:我正在输入:

$ 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

And getting this:得到这个:

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)

I pasted the code from the example into Example.java.我将示例中的代码粘贴到 Example.java 中。

UPDATE更新


A kind soul on #java at irc.freenode.net told me to chain my entries in the classpath. irc.freenode.net 上#java 上的好心人告诉我将我的条目链接到类路径中。 So, instead of:所以,而不是:

$ 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

I used:我用了:

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

Great: :-) But now new errors:太好了::-)但是现在出现了新的错误:

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

UPDATE更新


I tried:我试过:

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

But I see the org.openqa.selenium.By class, for example, so it doesn't appear that I'm missing any packages.但是我看到了 org.openqa.selenium.By class,例如,所以看起来我没有丢失任何包。

UPDATE更新


Okay: .-) I removed the package specifier: I executed:好的:.-) 我删除了 package 说明符:我执行了:

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

Which is what a commenter gave me, except I added the full path to the.jar.这是评论者给我的,只是我添加了 .jar 的完整路径。 Then I executed:然后我执行了:

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

Which gave me:这给了我:

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)

UPDATE更新


Hurray, Victory.万岁,胜利。 thanks to Mike Kwan: The following is what worked:感谢 Mike Kwan:以下是有效的:

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

BTW, I added the last part of the "-cp" option (for "libs/*"), because that's where HTMLUnit was when I downloaded the Selenium / WebDriver stuff.顺便说一句,我添加了“-cp”选项的最后一部分(对于“libs/*”),因为这是我下载 Selenium / WebDriver 东西时 HTMLUnit 所在的位置。

Compile like so:像这样编译:

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

Unless you put the Java file in the org.openqa.selenium.example , you will have to remove the package specifier.除非您将 Java 文件放入org.openqa.selenium.example中,否则您必须删除 package 说明符。 Run the compiled file like so:像这样运行编译后的文件:

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

This chains the current directory to the classpath.这会将当前目录链接到类路径。

For those doing a java dynamic web project in eclipse, and receiving the error above, the following link might help:对于那些在 eclipse 中执行 java 动态 web 项目并收到上述错误的人,以下链接可能会有所帮助:

http://me-ol-blog.blogspot.co.il/2013/07/using-selenium-in-java-dynamic-web.html http://me-ol-blog.blogspot.co.il/2013/07/using-selenium-in-java-dynamic-web.html

Basically, it mentions that the standalone jar is not "standalone" when using dynamic web projects and that a different jar should be used along with its dependent jars.基本上,它提到在使用动态 web 项目时,独立的 jar 不是“独立的”,并且应该与其相关的 jars 一起使用不同的 jar。

Add html unit driver dependency in your pom file.在您的 pom 文件中添加 html 单元驱动程序依赖项。

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>htmlunit-driver</artifactId>
    <version>2.21</version>
</dependency> 

It is working fine for me.它对我来说很好用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM