2011-09-16 101 views
17

所以我已經閱讀了所有關於在我的路徑上添加鉻綠色驅動程序的文檔,並遵循了所有這些文檔。我與selenium2,行家,日食在Mac上,所有最新的驅動程序:硒2鉻驅動程序

Error: 
The path to the chromedriver executable must be set by the webdriver.chrome.driver system property; 

我把chromedriver在我的應用程序文件夾,我的路徑是這樣的:

echo $PATH 
/Users/tcerrato/selenium/BS_Sel_Project/auto_helper/test_scripts:/usr/local/apache-maven-2.2.1//bin:/Users/oracle/oracle/product/10.2.0/db_1/bin:/opt/local/bin:/opt/local/sbin:/Applications: 

我缺少什麼?我無法使用chrome驅動程序運行。任何幫助將是偉大的我現在嘗試隨機的東西。

這裏是我的硒POM部分:

<dependency> 
    <groupId>org.seleniumhq.selenium</groupId> 
    <artifactId>selenium</artifactId> 
    <version>2.0rc2</version> 
    <type>pom</type> 
</dependency> 
<dependency> 
    <groupId>org.seleniumhq.selenium</groupId> 
    <artifactId>selenium-chrome-driver</artifactId> 
    <version>2.5.0</version> 
</dependency> 
<dependency> 
    <groupId>org.seleniumhq.selenium</groupId> 
    <artifactId>selenium-firefox-driver</artifactId> 
    <version>2.6.0</version> 
</dependency> 

回答

20

我不知道關於Maven但我怎麼設置webdriver.chrome.driver

System.setProperty("webdriver.chrome.driver", "C:\\pathto\\my\\chromedriver.exe"); 
WebDriver driver = new ChromeDriver(); 
driver.get("http://www.google.com"); 
+1

工作。沒有什麼我讀的,甚至遠遠地說明了它幾乎變得簡單:)感謝 – ducati1212

+2

如何在python中做到這一點? –

+1

@GregGauthier根據[chromedriver的「入門」頁面),您可以在創建webdriver時指定路徑作爲(可選)參數:'driver = webdriver.Chrome('/ path/to/chromedriver')' – zpea

0

試試這個屬性:

System.setProperty("webdriver.chrome.driver","/location to/chromedriver folder"); 
WebDriver driver = new ChromeDriver(); 
driver.get("your.app"); 
9

通過maven設置webdriver.chrome.driver系統屬性可以通過以下方式完成(並且測試工作):

  1. 添加systemPropertyVariables配置到maven-surefire-pluginpom.xml。這是(通常),因爲surefire是測試的調用者,並且系統屬性將被設置。

    <plugin> 
        <artifactId>maven-surefire-plugin</artifactId> 
        <version>2.7.1</version> 
        <configuration> 
         <systemPropertyVariables> 
          <webdriver.chrome.driver>${webdriver.chrome}</webdriver.chrome.driver> 
         </systemPropertyVariables> 
        </configuration> 
    </plugin> 
    
  2. 現在在某處定義${webdriver.chrome}。良好的開端是在你的pom.xml

    <properties> 
        <webdriver.chrome>/home/gede/bin/chromedriver</webdriver.chrome> 
    </properties> 
    

可能會給<properties>節這可以通過使用<profiles>像西蒙·馬蒂內利在你的POM做得更好example

0
System.setproperty("webdriver.chrome.driver","your file path here with chromedriver.exe"); 
webDriver driver=new chromeDriver(); 
driver.get("http://google.com"); 
2

所以像這樣設置

    <dependency> 
        <groupId>org.seleniumhq.selenium</groupId> 
        <artifactId>selenium-chrome-driver</artifactId> 
        <version>2.34.0</version> 
        </dependency> 

這是使用硒運行鉻的java代碼

 System.setProperty("webdriver.chrome.driver","C:/chromedriver.exe"); 
     WebDriver myD = new ChromeDriver(); 

爲了讓您運行Chrome,您需要從這裏下載chrome驅動程序。 https://code.google.com/p/chromedriver/downloads/list

一旦你完成了那麼你就必須在環境變量中設置它。閱讀本https://code.google.com/p/selenium/wiki/ChromeDriver

感謝,

 Mediha 
+1

那麼爲什麼甚至使用Maven依賴關係呢? –

4

你可以在使用驅動程序二進制文件下載Maven插件下載的驅動程序二進制文件,你一展身手(https://github.com/Ardesco/selenium-standalone-server-plugin):

   <plugin> 
        <groupId>com.lazerycode.selenium</groupId> 
        <artifactId>driver-binary-downloader-maven-plugin</artifactId> 
        <version>1.0.7</version> 
        <configuration> 
         <rootStandaloneServerDirectory>${project.basedir}/src/test/resources/selenium_standalone_binaries</rootStandaloneServerDirectory> 
         <downloadedZipFileDirectory>${project.basedir}/src/test/resources/selenium_standalone_zips</downloadedZipFileDirectory>        
        </configuration> 
        <executions> 
         <execution> 
          <goals> 
           <goal>selenium</goal> 
          </goals> 
         </execution> 
        </executions> 
       </plugin> 

這將下載二進制文件和設置,你可以在你的神火/故障安全配置中使用這樣的行家屬性:

   <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-failsafe-plugin</artifactId> 
        <version>2.7.2</version> 
        <configuration>        
         <systemProperties>        
          <!--Set properties passed in by the driver binary downloader--> 
          <phantomjs.binary.path>${phantomjs.binary.path}</phantomjs.binary.path> 
          <webdriver.chrome.driver>${webdriver.chrome.driver}</webdriver.chrome.driver> 
          <webdriver.ie.driver>${webdriver.ie.driver}</webdriver.ie.driver> 
          <webdriver.opera.driver>${webdriver.opera.driver}</webdriver.opera.driver> 
         </systemProperties> 
         <includes> 
          <include>**/*WebDriver.java</include> 
         </includes> 
        </configuration> 
        <executions> 
         <execution> 
          <goals> 
           <goal>integration-test</goal> 
           <goal>verify</goal> 
          </goals> 
         </execution> 
        </executions> 
       </plugin> 

當你實例化一個新的驅動程序對象的系統屬性指向驅動程序二進制文件的位置,將被設置,這將只是工作。

26

加入這個依賴於你的項目:

<dependency> 
    <groupId>io.github.bonigarcia</groupId> 
    <artifactId>webdrivermanager</artifactId> 
    <version>2.1.0</version> 
</dependency> 

這個庫下載最新的版本中,你需要的webdriver的二進制和導出正確的Java系統變量(webdriver.chrome.driverwebdriver.gecko.driverwebdriver.opera.driverphantomjs.binary.pathwebdriver.edge.driverwebdriver.ie.driver) ,只需分別使用下面的句子之一:

WebDriverManager.chromedriver().setup(); 
WebDriverManager.firefoxdriver().setup(); 
WebDriverManager.operadriver().setup(); 
WebDriverManager.phantomjs().setup(); 
WebDriverManager.edgedriver().setup(); 
WebDriverManager.iedriver().setup(); 

更多信息上https://github.com/bonigarcia/webdrivermanager

+0

我試過使用你的插件,但它下載適當的二進制文件似乎,即使在使用系統屬性驅動類org.jbehave.web.selenium時,jbehave總是嘗試使用該修補程序驅動程序。PropertyWebDriverProvider – berimbolo

+0

確定Jbehave需要設置一個額外的系統屬性:System.setProperty(「browser」,「chrome」);如果您的設置允許您指定要使用Jbehave,然後以您的名義設置其他屬性,但如果我自己設置此屬性,它確實可以很好地工作,那將會很好。謝謝 – berimbolo

0

只需添加WebDriverManager在你的Maven POM,如果你有在默認配置瀏覽器的設置它的工作原理,無需手動設置。

-1
Pom.xml code and Selenium code below: 


    <groupId>com.HelloWorld</groupId> 
    <artifactId>t</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>jar</packaging> 

    <name>t</name> 
    <url>http://maven.apache.org</url> 

    <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 




    <webdriver.chrome>/home/gede/bin/chromedriver</webdriver.chrome> 

    </properties> 

    <build> 
    <resources> 
     <resource> 
      <directory>src/main/java/resources</directory> 
      <filtering>true</filtering> 
     </resource> 
     </resources> 
     <plugins> 

     <plugin> 
     <artifactId>maven-surefire-plugin</artifactId> 
     <version>2.7.1</version> 
     <configuration> 
     <systemPropertyVariables> 
      <webdriver.chrome.driver>${webdriver.chrome} 
     </webdriver.chrome.driver> 
     </systemPropertyVariables> 
     </configuration> 
     </plugin> 


     <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-surefire-plugin</artifactId> 
     <version>2.20</version> 
     <configuration> 
      <suiteXmlFiles> 
      <suiteXmlFile>testng.xml</suiteXmlFile> 
      </suiteXmlFiles> 
     </configuration> 
     </plugin> 

     </plugins> 


     </build> 
     <dependencies> 

     <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>3.8.1</version> 
     <scope>test</scope> 
     </dependency> 

     <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium- 
    chrome-driver --> 
    <dependency> 
    <groupId>org.seleniumhq.selenium</groupId> 
    <artifactId>selenium-chrome-driver</artifactId> 
    <version>3.8.1</version> 
    </dependency> 



     <dependency> 
     <groupId>org.seleniumhq.selenium</groupId> 
     <artifactId>selenium-java</artifactId> 
     <version>3.4.0</version> 
     </dependency> 
     <dependency> 
    <groupId>org.testng</groupId> 
    <artifactId>testng</artifactId> 
     <version>6.8</version> 
    <scope>test</scope> 
    </dependency> 

    <dependency> 
      <groupId>org.seleniumhq.selenium</groupId> 
      <artifactId>selenium-chrome-driver</artifactId> 
      <version>3.8.1</version> 
     </dependency> 

     <dependency> 
     <groupId>io.github.bonigarcia</groupId> 
     <artifactId>webdrivermanager</artifactId> 
     <version>2.1.0</version> 
     </dependency> 



     <dependency> 
     <groupId>com.relevantcodes</groupId> 
     <artifactId>extentreports</artifactId> 
    <version>2.41.2</version> 
    </dependency> 



     <dependency> 
     <groupId>org.apache.logging.log4j</groupId> 
     <artifactId>log4j-api</artifactId> 
     <version>2.8.2</version> 
     </dependency> 
     <dependency> 
     <groupId>org.apache.logging.log4j</groupId> 
     <artifactId>log4j-core</artifactId> 
     <version>2.8.2</version> 
     </dependency> 
    </dependencies> 
</project> 


Selenuim Code 

public class App 
{ 
static String currentDir = System.getProperty("user.dir"); 
static WebDriver driver; 

    @BeforeClass 
    public static void setupClass() { 
     ChromeDriverManager.getInstance().setup(); 
     driver= new ChromeDriver(); 
     driver.get("https://www.google.com/"); 
    } 


@Test 
    public void test() { 





    System.out.println("Hello World!"); 

    } 
    }