3

So I'm trying to run my script through a command line so it works on other computers.

The problem is that I have this hard-coded into it.

System.setProperty("webdriver.chrome.driver","/Users/xxxx/Desktop/chromedriver");

I want to be able to run it without that line so I can just call the driver through the command line, is that possible?

This is what I was attempting to use to try and run it.

java -jar ~/Desktop/driver.jar -Dwebdriver.chrome.driver=/Users/xxxx/Desktop/Jar/chromedriver 

But I get an error telling me the driver does not exist.

2

You can get the driver path from the command line.

If you are using Java:

public static void main(String[] args) {
    System.setProperty("webdriver.chrome.driver", args[0]);
}

Or you can distribute a Chrome driver in your project and call it using relative path.

String projectPath = System.getProperty("user.dir");
String projectPathToTheDriver = ...;
System.setProperty("webdriver.chrome.driver",projectPath + projectPathToTheDriver + "chromedriver");
  • I've tried to use your code in the first line but I get an error. Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 – Christian Dec 29 '16 at 19:41
  • Have you passed the path as an argument to the Java program? Something like: java yourMainClassName /Users/userName/Desktop/chromedriver docs.oracle.com/javase/tutorial/essential/environment/… If you just call java yourMainClassName, the args arrays will be empty - because you passed no argument. – João Farias Jan 1 '17 at 20:07
  • This answer doesn't address the question, which was how to do this from the command line. i.e. if you're a PHP dev using Codeception this is wrong. – Jim Maguire Jul 23 '17 at 21:24
  • Jim, your comment makes no sense. The question asks how to make a Java program deal with arguments passed through command-line. If you are using other technologies, you will probably have a different approach. – João Farias Jul 24 '17 at 14:06
0

Came back to this question a lot later. I ended up coming up with a solution to run my script through a command line to start the server.

java -Dwebdriver.chrome.driver=chromedriver -jar selenium-server-standalone-3.3.1.jar for windows just add a .exe after chromedriver

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.