Ne peut pas exécuter Java exemple pour le Sélénium / WebDriver

Avoir un problème se passe avec le Sélénium sur l'île de Java. Je suis en train de suivre l'exemple sur cette page: http://code.google.com/p/selenium/wiki/GettingStarted

Je suis en train de taper ceci:

$ 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

Et obtenir ceci:

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)

J'ai collé le code de l'exemple dans Example.java.

Mise à JOUR


Une âme charitable sur #java à irc.freenode.net m'a dit à la chaîne de mes entrées dans le classpath. Ainsi, au lieu de:

$ 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

J'ai utilisé:

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

Grand! 🙂 Mais aujourd'hui de nouvelles erreurs:

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

Mise à JOUR


J'ai essayé:

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

Mais je vois la org.openqa.selenium.By de la classe, par exemple, de sorte qu'il n'apparaît pas que je suis absent tout les paquets.

Mise à JOUR


D'accord! 🙂 J'ai supprimé le paquet spécificateur. J'ai exécuté:

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

Qui est ce qu'un intervenant m'a donné, sauf que j'ai ajouté le chemin d'accès complet à l' .jar. Puis j'ai exécuté:

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

Qui m'a donné:

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)

Mise à JOUR


Hourra! La victoire, grâce à Mike Kwan. Voici ce qui a fonctionné:

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, j'ai ajouté la dernière partie de la "cp" (pour "libs/*"), parce que c'est là interface htmlunit a été lorsque j'ai téléchargé le Sélénium /WebDriver choses.

Je pense que votre "Example.class" le fichier est dans le répertoire incorrect, il doit être dans le org.openqa.le sélénium.un exemple.

OriginalL'auteur hourback | 2012-04-07