2012-12-05 108 views
121

我想这如何在Chrome中运行Selenium WebDriver测试用例?

WebDriver driver = new ChromeDriver(); 

,但我得到的错误作为

失败的测试:SETUP(com.TEST):给司机可执行文件的路径必须由webdriver.chrome设置.driver系统属性;有关更多信息,请参阅http://code.google.com/p/selenium/wiki/ChromeDriver。最新版本可从http://code.google.com/p/chromedriver/downloads/list下载

我该如何让Chrome测试Selenium-WebDriver测试用例?

+0

如果您知道您是否从http://code.google.com/p/chromedriver/downloads/list下载了硒铬驱动程序并将其添加到eclipse中的“添加外部罐子”下的库中,在运行你的selenium-java代码之前。 – Hemanth

回答

204

您需要从下载的可执行文件驱动程序:

System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver"); 
WebDriver driver = new ChromeDriver(); 

ChromeDriver Download

然后,所有你需要做的是创建驱动程序对象(以正确的顺序已经显示)之前,请使用以下这是从最有用的指南中提取的:https://sites.google.com/a/chromium.org/chromedriver/

+1

非常感谢!这对我很好! –

+0

@aimbire:嘿,我面临同样的问题。我做了同样的事情,但你得到这个错误。 “java.lang.IllegalStateException:驱动程序可执行文件不存在:D:\ selenimPRJarg1 \ chromedriver.exe”我是否缺少某些东西“? – kTiwari

+0

您必须从这里下载Selenium Standalone Server http://docs.seleniumhq。org/download /,并将jar文件作为依赖项添加到Java项目中。 – dikirill

15

您应该将chromeDriver下载到一个文件夹中,然后将此文件夹添加到PATH变量中。 您必须重新启动控制台才能使其正常工作。

19

here下载更新版本的chrome驱动程序

public class Chrome { 

    public static void main(String[] args) { 

     System.setProperty("webdriver.chrome.driver", "E://chromedriver.exe"); 
     WebDriver driver = new ChromeDriver();    
     driver.get("http://www.google.com"); 

    } 

} 

您也可以看到单击here

+3

现在这是一个麻烦的解决方案。如果这是你现在使用的,我建议你改变它。 – aimbire

+1

下载链接现在已过时。 –

-2

您需要下载Chrome驱动程序的exe文件后的答案。 您可以从此链接下载。

https://sites.google.com/a/chromium.org/chromedriver/

System.setProperty("webdriver.chrome.driver", "path of chromedriver.exe"); 

ChromeDriver driver = new ChromeDriver();  
+0

下载chromedriver并提取它。接下来在您的项目中创建一个文件夹,并将其命名为chromedriver并将.exe文件保存在其中。并使用此路径。这是简单的代码示例。 'code'System.setProperty(“webdriver.chrome.driver”,System.getProperty(“user.dir”)+“\\ chromedriver \\ chromedriver.exe”); \t \t WebDriver driver = new ChromeDriver(); \t \t driver.get(“http://www.google.co.in”); \t \t WebElement searchbox = driver.findElement(By.name(“q”)); (“最佳硒视频教程免费”); \t \t searchbox.submit(); – vinay

+0

请考虑更新此答案,并附加info vinay评论。 – rcdmk

4

您需要安装Chrome驱动程序。如下图所示

+1

这可能是一个,但NuGet的版本太旧:2.10.0日期2014年10月13日 –

+0

3月23日再次过期。无法在最新的Chrome上使用。 – willem

6

找到最新版本的chromedriverhere您可以安装使用金块这个包。 下载后,将其解压缩到python安装的根目录下,例如C:/Program Files/Python-3.5,就是这样。 您甚至不需要指定任何位置的路径和/或将chromedriver添加到您的路径等。 我只是干净的Python安装,它的工作原理。

+2

不好的建议......不要污染你的python安装 –

+0

另外,OP从未指定Python –

4

您可以使用下面的代码使用Selenium网络驱动程序在Chrome运行测试用例:

import java.io.IOException; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.chrome.ChromeDriver; 

public class ChromeTest { 

    /** 
    * @param args 
    * @throws InterruptedException 
    * @throws IOException 
    */ 
    public static void main(String[] args) throws InterruptedException, IOException { 
     // Telling the system where to find the Chrome driver 
     System.setProperty(
       "webdriver.chrome.driver", 
       "E:/chromedriver_win32/chromedriver.exe"); 

     WebDriver webDriver = new ChromeDriver(); 

     // Open google.com 
     webDriver.navigate().to("http://www.google.com"); 

     String html = webDriver.getPageSource(); 

     // Printing result here. 
     System.out.println(html); 

     webDriver.close(); 
     webDriver.quit(); 
    } 
} 
3

下载最新的Chrome驱动程序的版本,并使用此代码:

System.setProperty("webdriver.chrome.driver", " path of chromedriver.exe"); 
WebDriver driver= new ChromeDriver(); 
driver.manage().window().maximize(); 
Thread.sleep(10000); 
driver.get("http://stackoverflow.com"); 
5

如果”重新在MacOS上使用自制软件,你可以使用命令:

brew install chromedriver 

它应该工作正常后,没有其他配置。

0

以上所有答案都是正确的,以下是对问题和解决方案的深入探讨。

中硒的驱动程序构造例如

WebDriver driver = new ChromeDriver(); 

搜索驱动程序可执行文件,在镀铬驱动程序可执行这种情况下的镀铬驱动程序搜索,在情况下,服务是无法找到的异常被抛出

可执行

这是那里的例外来自(注意检查状态的方法)

/** 
    * 
    * @param exeName Name of the executable file to look for in PATH 
    * @param exeProperty Name of a system property that specifies the path to the executable file 
    * @param exeDocs The link to the driver documentation page 
    * @param exeDownload The link to the driver download page 
    * 
    * @return The driver executable as a {@link File} object 
    * @throws IllegalStateException If the executable not found or cannot be executed 
    */ 
    protected static File findExecutable(
     String exeName, 
     String exeProperty, 
     String exeDocs, 
     String exeDownload) { 
    String defaultPath = new ExecutableFinder().find(exeName); 
    String exePath = System.getProperty(exeProperty, defaultPath); 
    checkState(exePath != null, 
     "The path to the driver executable must be set by the %s system property;" 
      + " for more information, see %s. " 
      + "The latest version can be downloaded from %s", 
      exeProperty, exeDocs, exeDownload); 

    File exe = new File(exePath); 
    checkExecutable(exe); 
    return exe; 
    } 

以下是检查STA该引发异常

/** 
    * Ensures the truth of an expression involving the state of the calling instance, but not 
    * involving any parameters to the calling method. 
    * 
    * <p>See {@link #checkState(boolean, String, Object...)} for details. 
    */ 
    public static void checkState(
     boolean b, 
     @Nullable String errorMessageTemplate, 
     @Nullable Object p1, 
     @Nullable Object p2, 
     @Nullable Object p3) { 
    if (!b) { 
     throw new IllegalStateException(format(errorMessageTemplate, p1, p2, p3)); 
    } 
    } 

SOLUTION TE方法:创建驱动程序对象如下

System.setProperty("webdriver.gecko.driver", "path/to/chromedriver.exe"); 
WebDriver driver = new ChromeDriver(); 

以下之前设置系统属性是代码片段(对于铬和Firefox),其中驱动器服务的搜索驱动程序可执行文件:

铬:

@Override 
    protected File findDefaultExecutable() { 
     return findExecutable("chromedriver", CHROME_DRIVER_EXE_PROPERTY, 
      "https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver", 
      "http://chromedriver.storage.googleapis.com/index.html"); 
    } 

的FireFox:

@Override 
protected File findDefaultExecutable() { 
     return findExecutable(
     "geckodriver", GECKO_DRIVER_EXE_PROPERTY, 
     "https://github.com/mozilla/geckodriver", 
     "https://github.com/mozilla/geckodriver/releases"); 
    } 

其中CHROME_DRIVER_EXE_PROPERTY = “webdriver.chrome.driver” 和GECKO_DRIVER_EXE_PROPERTY = “webdriver.gecko.driver”

类似为其他浏览器的情况下,以下是可用浏览器实施列表的快照

enter image description here

相关问题