2012-04-07 135 views
8

在Java上使用Selenium時遇到問題。我試圖按照此頁面上的例子:http://code.google.com/p/selenium/wiki/GettingStarted無法爲Selenium/WebDriver運行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 

並獲得這樣的:

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) 

我粘貼代碼的例子爲示例的.java。

UPDATE


上在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 

UPDATE


我想:

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

但我看到了org.openqa.selenium.By類,例如,使它似乎並不是我缺少任何軟件包。

UPDATE


好吧! :-)我刪除了包說明符。我執行了:

$ 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) 

UPDATE


萬歲!勝利,感謝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,我添加了「-cp」選項的最後一部分(「庫/ *」),因爲那裏的HtmlUnit是當我下載的硒/ webdriver的東西。

+0

我認爲你的「Example.class」文件位於錯誤的目錄中,它應該位於org.openqa.selenium.example之一。 – mglauche 2012-04-07 18:36:45

回答

12

編譯如下所示:

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

除非你把org.openqa.selenium.example Java文件,你將不得不刪除軟件包符。運行編譯的文件,如下所示:

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

這將當前目錄鏈接到類路徑。

+0

謝謝您的建議!我用結果更新了我的問題。 – hourback 2012-04-08 00:33:14

+1

嘗試使用完整路徑而不是〜 – 2012-04-08 00:46:12

+0

Mike,爲什麼示例代碼的作者在這種情況下會提供包說明符? – hourback 2012-04-08 04:51:08

0

在您的pom文件中添加html單元驅動程序依賴項。

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

它對我來說工作正常。