2013-04-01 37 views
0

我正在使用Netbeans。我添加了一個庫selenium-java-2.31.0。但它顯示異常。 我添加了這個庫所依賴的所有庫。在Maven中添加Selenium-java-2.31.0庫後Java代碼拋出異常

我按照這個link在netbeans中添加庫。

我的代碼: -

import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 
import org.jsoup.select.Elements; 
import java.io.IOException; 
import java.net.URI; 
import java.net.URISyntaxException; 
import java.util.Iterator; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 


public static void main(String[] args) throws IOException, URISyntaxException { 
     String url1 = "http://www.jabong.com/giordano-P6868-Black-Analog-Watch-183702.html"; 


     Document doc1 = Jsoup.connect(url1).get(); 

     WebDriver driver = new FirefoxDriver(); 
     driver.get(url1); 


     Elements tag_list = doc1.getAllElements(); 

     for(Element tag1 : tag_list) { 
      Point point=driver.findElement(By.id(tag1.id())).getLocation(); 
      System.out.println("X Position : " + point.x); 
      System.out.println("Y Position : " + point.y); 

     } 
} 

異常

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Function 
    at template_matching.Template_matching.main(Template_matching.java:275) 
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366) 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356) 
    ... 1 more 
Java Result: 1 
BUILD SUCCESSFUL (total time: 2 seconds) 

幫我解決這個問題。爲什麼這個異常會拋出?

編輯1號

Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONException 
    at org.openqa.selenium.firefox.FirefoxProfile.<init>(FirefoxProfile.java:89) 
    at org.openqa.selenium.firefox.FirefoxProfile.<init>(FirefoxProfile.java:79) 
    at org.openqa.selenium.firefox.FirefoxProfile.<init>(FirefoxProfile.java:67) 
    at org.openqa.selenium.firefox.FirefoxDriver.getProfile(FirefoxDriver.java:260) 
    at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:236) 
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:110) 
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:190) 
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:183) 
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:179) 
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:92) 
    at template_matching.Template_matching.main(Template_matching.java:275) 
Caused by: java.lang.ClassNotFoundException: org.json.JSONException 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366) 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356) 
    ... 11 more 
Java Result: 1 

編輯二號

現在我搬到Maven的時候,通過思考它下載所有的依賴庫。但現在它顯示了不同的錯誤。

enter image description here

編輯3

號,因爲它顯示了一些問題與Firefox,我取代火狐瀏覽器。 這次以前的錯誤已被刪除。但是一幅新的照片出現了。請看這個。

enter image description here

編輯沒有。 4

enter image description here

編輯第5號

現在我轉移到Firefox再次,並得到給定的網頁,但它stucks然後。爲什麼?

下面是我得到的錯誤快照。

enter image description here

+1

您需要['Google Guava'](http://code.google。com/p/guava-libraries /)在你的類路徑中的外觀。這是「com.google.common.base.Function」類所在的地方。 –

+0

@ bmorris591現在它顯示新的異常。請參閱編輯編號1. – devsda

+0

另一個缺失的類,這次是'org.json.JSONException'。谷歌它,看看它來自哪裏。 –

回答

3

我相信你在這一點上所看到的問題是由於您在driver.get(「URL1」)傳遞;

我想你的意思是傳遞給driver.get(url1); (注意不帶引號)

爲了擴展它一點,它拋出格式錯誤的uri異常的原因是uri被傳入(url1)不包含任何協議(http/https)。

+0

感謝您的不錯觀察。但它再次表明了一些例外。請參閱編輯號碼。 4,請。 – devsda

+0

我還在路徑變量中添加了'chrome.exe'絕對路徑。 – devsda

+0

我沒有看到很多關於你如何在你的項目中設置的chromedriver。在我的第一個問題之前設置好之後,您是否會下載並安裝服務器文件?我會在這裏查看說明:http://code.google.com/p/selenium/wiki/ChromeDriver,並且在跟蹤他們之後,看看您是否仍然有異常。 Chromedriver與firefox有點不同,它需要多個文件才能真正被使用。瀏覽器本身和服務器以及服務器也必須位於路徑中。 –

0

我想你可能已經創建了一個maven項目,它將解決你的與硒相關的依賴問題。

+0

我已經在maven工作。請參閱編輯號碼。 5,它顯示了我原來的問題。 – devsda