## 浏览器操作
### 打开浏览器
打开默认路径的firefox
```
WebDriver driver = new FirefoxDriver();
```
打开指定路径的firefox
```
System.serProperty("webdriver. firefox.bin",
"C:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
```
或者
```
File pathToFirefoxBinary
= new File("C:\\Program Files\\Mozilla Firefox\\firefox.exe");
FirefoxBinary firefoxbin = new FirefoxBinary(pathToFirefoxBinary);
WebDriver driver = new FirefoxDriver(firefoxbin,null);
```
打开ie(需要驱动)
```
System.setProperty("webdriver.ie.driver", "...\\IEDriverServer.exe")
WebDriver driver = new InternetExplorerDriver();
```
打开chrome(需要驱动)
```
System.setProperty("webdriver.chrome.driver", "...\\chromedriver.exe" );
System.setProperty("webdriver.chrome.bin",
"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe");
WebDriver driver = new ChromeDriver();
```
### 打开URL
用get方法
```
driver.get("http://www.51.com");
```
或者用navigate方法,然后再调用to方法
```
driver.navigate().to("http://www.51.com");
```
### 关闭浏览器
用quit方法
```
driver.quit();
```
或者用close方法
```
driver.close();
```
### 返回当前页面url和title
得到title
```
String title = driver.getTitle();
```
得到当前页面url
```
String currentUrl = driver.getCurrentUrl();
```
输出title和currenturl
```
System.out.println(title+"\n"+currentUrl);
```
### 其他方法
* `getWindowHandle()` 返回当前的浏览器的窗口句柄
* `getWindowHandles()` 返回当前的浏览器的所有窗口句柄
* `getPageSource()` 返回当前页面的源码