WebDriver driver = new FirefoxDriver();
WebDriver driver = new InternetExplorerDriver ();
WebDriverdriver = new HtmlUnitDriver();
WebDriverdriver = new ChromeDriver();
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
WebDriver driver = new FirefoxDriver();
P.S.navigate方法会产生1个Navigator对象,其封装了与导航相关的一些方法,比如前进后退等
Webdriver提供下面两种方法来定位页面元素,参数是By对像,最常用是By.id和By.name查找。
例如需要定位如下元素:
<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();