Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I load the ChromeDriver binary from the classpath?

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?

like image 312
Adam Burley Avatar asked May 05 '15 20:05

Adam Burley


1 Answers

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());
like image 53
Adam Burley Avatar answered Sep 29 '22 11:09

Adam Burley