Semua jawaban di atas benar, berikut adalah sedikit menyelam jauh ke dalam masalah dan solusinya.
Konstruktor pengemudi di selenium misalnya
WebDriver driver = new ChromeDriver();
mencari driver executable, dalam hal ini driver chrome mencari driver executable chrome, dalam hal layanan tidak dapat menemukan executable pengecualian dilemparkan
ini adalah tempat pengecualian berasal (perhatikan metode check state)
/**
*
* @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;
}
Berikut ini adalah metode keadaan cek yang melemparkan pengecualian
/**
* 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));
}
}
SOLUSI : atur properti sistem sebelum membuat objek driver sebagai berikut
System.setProperty("webdriver.gecko.driver", "path/to/chromedriver.exe");
WebDriver driver = new ChromeDriver();
berikut ini adalah cuplikan kode (untuk chrome dan firefox) tempat layanan driver mencari driver yang dapat dieksekusi:
Chrome:
@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");
}
di mana CHROME_DRIVER_EXE_PROPERTY = "webdriver.chrome.driver" dan GECKO_DRIVER_EXE_PROPERTY = "webdriver.gecko.driver"
serupa halnya dengan browser lain, berikut ini adalah snapshot dari daftar implementasi browser yang tersedia