Step1: 下載chromedriver。
下載路徑: http://chromedriver.storage.googleapis.com/index.html
選擇一個合適的下載即可。我下載的是2.20版本的chromedriver_win32.zip ,解壓之后得到chromedriver.exe 。
Step2: 放置chromedriver.exe
在跟項目包平行的地方新建一個包,比如”lib",然后將 chromedriver.exe拷貝到lib下。
Step3: 添加 webdriver.chrome.driver 屬性設置
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class ChromeTest { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","src/lib/chromedriver.exe"); WebDriver wd = new ChromeDriver(); wd.get("http://www.baidu.com"); try { Thread.sleep(1200); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(wd.getCurrentUrl()); wd.quit(); } }
Step4: 運行
Starting ChromeDriver 2.20.353145 (xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx) on port 14153
Only local connections are allowed.
https://www.baidu.com/