12

I'm writing a web-automation program and so far, it works fine using Selenium's FirefoxDriver. However, I want to make it use Chrome if Firefox is not installed. I downloaded the ChromeDriver, put it inside a folder in my Eclipse project, and ran it. After I added System.setProperty("webdriver.chrome.driver", "Drivers/chromedriver.exe"); it worked fine. However, when I try to export it (using Eclipse's standard Export). It crashes, I believe because it can't find the ChromeDriver (I think this because exporting it with FirefoxDriver works fine).

I have tried changing the .JAR to a .ZIP in order to look inside, and I see that the driver folder was stripped away, simply putting chromedriver.exe inside the top-level JAR. I tried changing the property to System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); but still crashes.

Does anyone know why the exported JAR is unable to find chromeDriver, despite the fact that I have verified that it is in the JAR?

Thanks, Daniel

DaniChan
  • 145
  • 1
  • 1
  • 7

3 Answers3

7

you have to extract it before running it. Copy Your driver under src/resources/drivers/chromedriver.exe

Now whenever you run this code , it will Create a new Folder "Driver" where ever you put your Runnable jar and copy your driver, which you can access.

ClassLoader classLoader = getClass().getClassLoader();
                URL resource = classLoader.getResource("resources/drivers/chromedriver.exe");
                File f = new File("Driver");
                if (!f.exists()) {
                    f.mkdirs();
                }
                File chromeDriver = new File("Driver" + File.separator + "chromedriver.exe");
                if (!chromeDriver.exists()) {
                    chromeDriver.createNewFile();
                    org.apache.commons.io.FileUtils.copyURLToFile(resource, chromeDriver);
                }
                System.setProperty("webdriver.chrome.driver", chromeDriver.getAbsolutePath());
                driver = new ChromeDriver();
user1565473
  • 71
  • 1
  • 4
4

Export your code as jar without the chromedriver. Create a folder ChromeDriver. Place your chromedriver.exe in this folder. Place ChromeDriver folder along with your jar.

Also dont forget to set the System property in the code to

System.setProperty("webdriver.chrome.driver", "ChromeDriver/chromedriver.exe"); 

Please let me know if this works for you.

StrikerVillain
  • 3,719
  • 2
  • 24
  • 41
  • 1
    Although I see no reason why this wouldn't work, I really would like to be able to allow the user to only have to download one type of file. This method should work, although the `jar` would no longer be self-sufficient. – DaniChan Feb 09 '14 at 21:18
  • This works. While it does not directly answer the question, it is a more elegant method. Maintaining the webdriver binaries should be the responsibility of the environment using it, as the JAR can be executed in different environments/platforms. You can simply package the JAR and the `chromedriver.exe` in a ZIP. – k_rollo Oct 14 '19 at 12:10
1

It's in the Jar but cannot be executed (same as trying to run an .exe from a zip file), you have to extract it before running it

Ittiel
  • 1,104
  • 9
  • 12
  • Ok, How would I go about doing that? – DaniChan Feb 09 '14 at 16:58
  • 3
    See example here: http://stackoverflow.com/questions/12531766/run-exe-file-from-inside-jar – Ittiel Feb 09 '14 at 21:13
  • THANKS! That worked! I had a slight problem of where to place the `chromedriver` in order for it to be found, but that was fixed when I put it in the `src` folder. Once again, Thanks! – DaniChan Feb 09 '14 at 21:19