2016-11-23 27 views
6

我試圖使用Eclipse下載硒網絡驅動器,我的最後一個步驟,併成功導入網絡驅動程序,但是,當我試圖做同樣的Firefox我沒有得到導入選項。有什麼建議麼?下面的代碼有什麼問題嗎?硒安裝障礙「importfirefoxdriver」

代碼:

package webdriver_project; 

import org.openqa.selenium.WebDriver; 

public class webdriver_module_1 { 
    public static void main(String[] args) { 
     WebDriver driver = new firefoxDriver(); 
    } 

} 

回答

2

如果您使用的是Firefox版本48或更高版本,您必須先下載木偶司機:
https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/WebDriver
選擇適合你的系統版本(在Windows/Linux操作系統,32位或64位),下載它並更新Path系統變量以將完整的目錄路徑添加到可執行文件。
見changelog的官方信息:https://github.com/SeleniumHQ/selenium/blob/master/dotnet/CHANGELOG

Geckodriver現在是自動化的Firefox的默認機制。 這是Mozilla的實現驅動程序的該瀏覽器, 和自動化所需的Firefox版本48及以上。


我不知道你怎麼使用eclipse下載硒。你從他們的頁面下載庫(罐),並手動將它們作爲使用Java構建路徑/庫選項在Eclipse外部罐子?

總之,在我看來,最簡單的方法是將項目轉化爲Maven項目:

  • 首先使用Eclipse市場選擇安裝Maven插件:在Eclipse中的項目http://www.eclipse.org/m2e/
  • 下一個點擊右鍵,然後選擇配置/轉換爲Maven項目。接下來編輯pom.xml文件和硒的網頁添加到它的依賴:http://docs.seleniumhq.org/download/maven.jsp

    <dependency> 
        <groupId>org.seleniumhq.selenium</groupId> 
        <artifactId>selenium-java</artifactId> 
        <version>3.0.1</version> 
    </dependency> 
    

pom.xml在我的示例項目的全部內容:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>WebKierowca</groupId> 
    <artifactId>WebKierowca</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <build> 
     <sourceDirectory>src</sourceDirectory> 
     <plugins> 
      <plugin> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.5.1</version> 
       <configuration> 
        <source>1.8</source> 
        <target>1.8</target> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
    <dependencies> 
     <dependency> 
      <groupId>org.seleniumhq.selenium</groupId> 
      <artifactId>selenium-java</artifactId> 
      <version>3.0.1</version> 
     </dependency> 
    </dependencies> 
</project> 

最後創建下面的Java類,變化指着木偶驅動程序(geckodriver.exe)的路徑,右擊這個類並運行它爲Java應用程序。如果一切正常,就應該啓動Firefox,去谷歌網頁,搜索詞「硒」,並顯示搜索結果,持續5秒:

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 

public class Test { 

    public static void main(String ... x){ 
     // Path to Marionette driver 
     System.setProperty("webdriver.gecko.driver", "C:/serwery/geckodriver.exe"); 

     WebDriver driver = new FirefoxDriver(); 

     driver.get("http://www.google.com"); 

     driver.findElement(By.name("q")).sendKeys("Selenium"); 
     driver.findElement(By.name("btnG")).click(); 

     try { 
      Thread.sleep(5000); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     driver.quit(); 
    } 
} 
+0

謝謝!有效! –