在腳本開頭要指定瀏覽器位置.
1 public static void main(String[] args) throws InterruptedException, IOException { 2 System.setProperty("webdriver.gecko.driver", "D:\\geckodriver-v0.19.1-win64\\geckodriver.exe"); 3 ProfilesIni pi = new ProfilesIni(); 4 FirefoxProfile profile = pi.getProfile("default"); 5 FirefoxOptions options = new FirefoxOptions(); 6 options.setProfile(profile); 7 WebDriver driver = new FirefoxDriver(options); 8 driver.manage().window().maximize(); 9 driver.get("http://www.baidu.com"); 10 System.out.println(driver.getTitle());
不需要在腳本中指定瀏覽器位置的情形:
1. 瀏覽器安裝在默認位置
2. 瀏覽器已添加在path中(i.e. 已配置環境變量)