WebDriver driver = new FirefoxDriver(); css
WebDriver driver = new InternetExplorerDriver ();html
WebDriverdriver = new HtmlUnitDriver(); java
WebDriverdriver = new ChromeDriver();git
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();web
WebDriver driver = new FirefoxDriver();chrome
P.S.navigate方法會產生1個Navigator對象,其封裝了與導航相關的一些方法,好比前進後退等api
Webdriver提供下面兩種方法來定位頁面元素,參數是By對像,最經常使用是By.id和By.name查找。瀏覽器
例如須要定位以下元素:app
<input class="input_class" type="text" name="passwd" id="passwd-id" /> 異步
WebElement element = driver.findElement(By.id("passwd-id"));
WebElement element = driver.findElement(By.name("passwd"));
WebElement element =driver.findElement(By.xpath("//input[@id='passwd-id']"));
WebElement element = driver.findElement(By.className("input_class"));
WebElement element = driver.findElement(By.cssSelector(".input_class"));
//通俗點就是精確查詢
WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com/");
WebElement element = driver.findElement(By.linkText("百科"));
//這個方法就是模糊查詢
WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com/");
WebElement element = driver.findElement(By.partialLinkText("hao"));
WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com/");
String test= driver.findElement(By.tagName("form")).getAttribute("name");
System.out.println(test);
WebElement element = driver.findElement(By.id("passwd-id"));
Select select = new Select(driver.findElement(By.id("select")));
WebElement radio=driver.findElement(By.id("BookMode"));
WebElement checkbox = driver.findElement(By.id("myCheckbox."));
WebElement btn= driver.findElement(By.id("save"));
Alert alert = driver.switchTo().alert();
Form中的元素的操做和其它的元素操做同樣,對元素操做完成後對錶單的提交能夠:
WebElement approve = driver.findElement(By.id("approve"));
approve.click();
或
approve.submit();//只適合於表單的提交
上傳文件的元素操做:
WebElement adFileUpload =driver.findElement(By.id("WAP-upload"));
String filePath = "C:\test\\uploadfile\\media_ads\\test.jpg";
adFileUpload.sendKeys(filePath);
Web driver對Java Script的調用是經過JavascriptExecutor來實現的,例如:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("JS腳本");
WebDriver driver = new FirefoxDriver();