A number of sources make reference to loading the ChromeDriver binary from the classpath, but I haven't worked out how to do it if the binary is not in the classpath root.
To specify the path for the binary it seems that you have to set a system property "webdriver.chrome.driver". First I tried:
System.setProperty("webdriver.chrome.driver", "drivers/Chrome/chromedriver.exe");
But I got an error, and it seems it was looking for the driver in the location "C:\<working directory of my application process>\drivers\Chrome\chromedriver.exe"
. Here the working directory was actually the directory where my source code is stored.
Then I tried:
System.setProperty("webdriver.chrome.driver", "/drivers/Chrome/chromedriver.exe");
However the same thing happened - this time it was looking in "C:\drivers\Chrome\chromedriver.exe"
.
How do I get ChromeDriver to look for the ChromeDriver binary on the classpath when using the "webdriver.chrome.driver" property or any other way of configuring it?
Ultimately, I found that ChromeDriver
doesn't support classpath-relative access to its binary. However, you can convert the classpath-relative string to a system path and then directly load it, bypassing the system property.
URL url = this.getClass().getClassLoader().getResource(classpathRelativeLocation);
File file = new File(url.getFile()); // Strangely, URL.getFile does not return a File
ChromeDriverService.Builder bldr = (new ChromeDriverService.Builder())
.usingDriverExecutable(file)
.usingAnyFreePort();
ChromeDriver driver = new ChromeDriver(bldr.build());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With