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");
}
}
/**
*
* @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;
}
以下是检查状态方法,它抛出异常
/**
* 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 :在创建驱动程序对象之前设置系统属性,如下所示
System.setProperty("webdriver.gecko.driver", "path/to/chromedriver.exe");
WebDriver driver = new ChromeDriver();
10 回答
您需要从以下位置下载可执行驱动程序:ChromeDriver Download
然后,您需要做的就是在创建驱动程序对象之前使用以下内容(已按正确的顺序显示):
这是从ChromeDriver Documentation中最有用的指南中提取的 .
从here下载镀铬驱动程序的更新版本
单击here后,您还可以看到答案
您应该在文件夹中下载chromeDriver,并在PATH变量中添加此文件夹 . 您必须重新启动控制台才能使其正常工作 .
如果您在MacOS上使用自制程序,则可以使用以下命令:
在没有其他配置的情况下它应该可以正常工作 .
您需要安装chrome驱动程序 . 您可以使用nugget安装此包,如下所示
查找
chromedriver
here的最新版本 . 下载后,将其解压缩到python安装的根目录,例如C:/Program Files/Python-3.5
,并且's it. You don'甚至需要在任何地方指定路径和/或将chromedriver
添加到您的路径等 . 我刚刚在一个干净的Python安装上做到了这一点 .您可以使用以下代码使用Selenium网络驱动程序在Chrome中运行测试用例:
下载最新版本的chrome驱动程序并使用此代码:
以上所有答案都是正确的,以下是对问题和解决方案的深入探讨 .
例如,selenium中的驱动程序构造函数
搜索驱动程序可执行文件,在这种情况下,chrome驱动程序搜索chrome驱动程序可执行文件,以防服务无法找到可执行文件时抛出异常
这是异常的来源(注意检查状态方法)
以下是检查状态方法,它抛出异常
SOLUTION :在创建驱动程序对象之前设置系统属性,如下所示
以下是驱动程序服务搜索驱动程序可执行文件的代码段(用于chrome和firefox):
Chrome:
FireFox:
其中CHROME_DRIVER_EXE_PROPERTY =“webdriver.chrome.driver”和GECKO_DRIVER_EXE_PROPERTY =“webdriver.gecko.driver”
类似于其他浏览器的情况,以下是可用浏览器实现列表的快照
下载chromedriver的exe并将其提取到当前项目位置 . 在这里链接,我们可以在这里下载最新版本的chromedriver .
https://sites.google.com/a/chromium.org/chromedriver/
这里是启动浏览器的简单代码并导航到url .