1.启动浏览器
详见:
HtmlUnitDriver
FirefDriver和InternetExplorerDriver
ChromeDriver
2.访问url
方法1:使用get
package selenium.test.googleSearch; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.*; public class BaiduFirefoxDriver { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe"); WebDriver driver = new FirefoxDriver(); //页面跳转 driver.get("http://www.baidu.com/"); } }
方法2:
package selenium.test.googleSearch; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.*; public class BaiduFirefoxDriver { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe"); WebDriver driver = new FirefoxDriver(); //页面跳转 driver.navigate().to("http://www.baidu.com/"); } }
3.关闭浏览器
package selenium.test.googleSearch; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.*; public class BaiduFirefoxDriver { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe"); WebDriver driver = new FirefoxDriver(); //页面跳转 driver.get("http://www.baidu.com/"); //关闭浏览器 driver.quit(); } }
4.获得页面元素
//得到title String title = driver.getTitle();
//得到当前页面url String currentUrl = driver.getCurrentUrl();
getWindowHandle() // 返回当前的浏览器的窗口句柄 getWindowHandles() // 返回当前的浏览器的所有窗口句柄 getPageSource() // 返回当前页面的源码
详细用法在 这里