Basically what I would like to do is to open ChromeDriver wherever it is on a users desktop without knowing the full path for reasons like not knowing the full path or the location of ChromeDriver is changed at some point in time. I am new to this so I really don't know much when it comes to this sort of problem.
Basically my current code looks like this:
System.setProperty("webdriver.chrome.driver", "C:\\Users\\abcde\\Desktop\\selenium\\Chromedriver.exe");
What I'm wondering if there is a way to open ChromeDriver without specifying the full path so it looks something like this:
System.setProperty("webdriver.chrome.driver", "...\\Chromedriver.exe");```
I know this isn't a correct way to write it I'm just giving a example of how I'm thinking it would look and this line came to mine since in html I used ../css
when I wanted to specify the path to my CSS file.
CodePudding user response:
Incase of a multiuser project where you are not sure about the location of the absolute location of the ChromeDriver an ideal approach would be to create a Properties object and initialize it from myProperties.txt
as follows:
Contents of
myProperties.txt
:webdriver.chrome.driver=C:\\BrowserDrivers\\chromedriver.exe
Now you can write a class PropertiesTest then use System.setProperties()
to install the new Properties objects as the current set of system properties.
import java.io.FileInputStream;
import java.util.Properties;
public class PropertiesTest {
public static void main(String[] args)
throws Exception {
// set up new properties object
// from file "myProperties.txt"
FileInputStream propFile =
new FileInputStream( "myProperties.txt");
Properties p =
new Properties(System.getProperties());
p.load(propFile);
// set the system properties
System.setProperties(p);
// display new properties
System.getProperties().list(System.out);
}
}