基本上,我想做的是在不知道完整路徑的情況下在用戶桌面上的任何位置打開 ChromeDriver,因為不知道完整路徑或 ChromeDriver 的位置在某個時間點發生了更改。我是新手,所以當涉及到這類問題時,我真的不太了解。
基本上我當前的代碼如下所示:
System.setProperty("webdriver.chrome.driver", "C:\\Users\\abcde\\Desktop\\selenium\\Chromedriver.exe");
我想知道是否有辦法在不指定完整路徑的情況下打開 ChromeDriver,所以它看起來像這樣:
System.setProperty("webdriver.chrome.driver", "...\\Chromedriver.exe");```
我知道這不是正確的撰寫方式我只是舉一個例子說明我的想法,這行是我的,因為在 html 中../css我想指定 CSS 檔案的路徑時使用.
uj5u.com熱心網友回復:
如果您不確定ChromeDriver的絕對位置的位置的多用戶專案,理想的方法是創建一個PropertiesmyProperties.txt物件并按如下方式對其進行初始化:
內容
myProperties.txt:webdriver.chrome.driver=C:\\BrowserDrivers\\chromedriver.exe
現在您可以撰寫一個類PropertiesTest,然后使用它System.setProperties()來安裝新的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);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/453389.html
上一篇:優化(在速度方面)
