selenium操作chrome浏览器需要有ChromeDriver驱动来协助。
System.setProperty("webdriver.chrome.driver", "C:\\Documents and Settings\\Administrator\\Local Settings\\Application Data\\Google\\Chrome\\Application\\chromedriver.exe"); |
WebDriver driver = new ChromeDriver(); driver.get("http://www.baidu.com/"); |
public static void main(String[] args) { // TODO Auto-generated method stub //设置访问ChromeDriver的路径 System.setProperty("webdriver.chrome.driver", "C:\\Documents and Settings\\Administrator\\LocalSettings\\Application Data\\Google\\Chrome\\Application\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("http://www.baidu.com/");
} |
OS | Expected Location of Chrome |
Linux | /usr/bin/google-chrome1 |
Mac | /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome |
Windows XP | %HOMEPATH%\Local Settings\Application Data\Google\Chrome\Application\chrome.exe |
Windows Vista | C:\Users\%USERNAME%\AppData\Local\Google\Chrome\Application\chrome.exe |
ChromeDriverService service = new ChromeDriverService.Builder() .usingChromeDriverExecutable(new File("E:\\Selenium WebDriver\\chromedriver_win_23.0.1240.0\\chromedriver.exe")).usingAnyFreePort().build();
service.start();
driver = new ChromeDriver();
driver.get("http://www.baidu.com");
driver.quit();
// 关闭 ChromeDriver 接口
service.stop();
http://www.testwo.com/blog/6931