我试过了
WebDriver driver = new ChromeDriver();
但我得到的错误
测试失败:setUp(com.TEST):必须通过webdriver.chrome.driver系统属性设置驱动程序可执行文件的路径;否则,请执行以下操作:有关更多信息,请参见此处的代码。可以从链接下载最新版本
如何使Chrome浏览器测试Selenium-WebDriver测试用例?
我试过了
WebDriver driver = new ChromeDriver();
但我得到的错误
测试失败:setUp(com.TEST):必须通过webdriver.chrome.driver系统属性设置驱动程序可执行文件的路径;否则,请执行以下操作:有关更多信息,请参见此处的代码。可以从链接下载最新版本
如何使Chrome浏览器测试Selenium-WebDriver测试用例?
Answers:
您需要从以下位置下载可执行驱动程序: ChromeDriver下载
然后,您要做的就是在创建驱动程序对象之前使用以下命令(已经以正确的顺序显示):
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
摘录自ChromeDriver文档中最有用的指南。
从铬驱动下载更新版本的Chrome驱动程序 请参阅发行文件以及这里 如果Chrome浏览器,然后更新您需要下载新的chormedriver从上面的链接,因为这将是紧凑的,能以新的浏览器版本。
public class chrome
{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
}
}
您可以使用以下代码通过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();
}
}
在chromedriver
此处找到最新版本。下载完成后,将其解压缩到python安装的根目录下C:/Program Files/Python-3.5
,仅此而已。您甚至不需要在任何地方指定路径和/或添加chromedriver
到路径等中。我只是在一个干净的Python安装上完成的,并且有效。
下载最新版的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");
上面的所有答案都是正确的,以下是对该问题和解决方案的深入探讨。
例如selenium中的驱动程序构造函数
WebDriver driver = new ChromeDriver();
搜索驱动程序可执行文件,在这种情况下chrome驱动程序搜索chrome驱动程序可执行文件,如果服务无法找到可执行文件,则会引发异常
这是异常的来源(请注意检查状态方法)
/**
*
* @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));
}
}
解决方案:在创建驱动程序对象之前,设置系统属性,如下所示
System.setProperty("webdriver.gecko.driver", "path/to/chromedriver.exe");
WebDriver driver = new ChromeDriver();
以下是驱动程序服务搜索驱动程序可执行文件的代码段(适用于chrome和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");
}
火狐:
@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”
其他浏览器也是如此,以下是可用浏览器实现列表的快照
To run Selenium WebDriver test cases in Chrome, follow these steps:
first of all set the property & chrome driver path
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
then initialize the Chrome Driver's object
WebDriver driver = new ChromeDriver();
then pass the url into get method of WebDriver
driver.get("http://www.google.com");
我将二进制文件包含在我的项目资源目录中,如下所示:
src\main\resources\chrome\chromedriver_win32.zip
src\main\resources\chrome\chromedriver_mac64.zip
src\main\resources\chrome\chromedriver_linux64.zip
码:
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.SystemUtils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.*;
import java.nio.file.Files;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public WebDriver getWebDriver() throws IOException {
File tempDir = Files.createTempDirectory("chromedriver").toFile();
tempDir.deleteOnExit();
File chromeDriverExecutable;
final String zipResource;
if (SystemUtils.IS_OS_WINDOWS) {
zipResource = "chromedriver_win32.zip";
} else if (SystemUtils.IS_OS_LINUX) {
zipResource = "chromedriver_linux64.zip";
} else if (SystemUtils.IS_OS_MAC) {
zipResource = "chrome/chromedriver_mac64.zip";
} else {
throw new RuntimeException("Unsuppoerted OS");
}
try (InputStream is = getClass().getResourceAsStream("/chrome/" + zipResource)) {
try (ZipInputStream zis = new ZipInputStream(is)) {
ZipEntry entry;
entry = zis.getNextEntry();
chromeDriverExecutable = new File(tempDir, entry.getName());
chromeDriverExecutable.deleteOnExit();
try (OutputStream out = new FileOutputStream(chromeDriverExecutable)) {
IOUtils.copy(zis, out);
}
}
}
System.setProperty("webdriver.chrome.driver", chromeDriverExecutable.getAbsolutePath());
return new ChromeDriver();
}
下载chromedriver的exe并将其解压缩到当前项目位置。在此处的链接中,我们可以在其中下载最新版本的chromedriver。
https://sites.google.com/a/chromium.org/chromedriver/
这里是启动浏览器的简单代码,并导航到url。
System.setProperty("webdriver.chrome.driver", "path of chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://any_url.com");