2011-09-16 30 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!"); 

    } 
    }