标签:
1、Alert类
public void testAlert(){ WebElement element = driver.findElement(By.className("alert")); element.click(); Alert alert = driver.switchTo().alert(); String text = alert.getText(); alert.accept(); //alert.dismiss(); System.out.println(text); }
使用Actions类
public void testAlertByActions(){ WebElement element = driver.findElement(By.className("alert")); Actions action = new Actions(driver); action.click(element).perform(); Alert alert = driver.switchTo().alert(); String text = alert.getText(); alert.accept(); //alert.dismiss(); System.out.println(text); }
2、Action类
public void testActions(){ WebElement element = driver.findElement(By.className("over")); Actions action = new Actions(driver); action.moveToElement(element).perform(); String text = driver.findElement(By.id("over")).getText(); System.out.println(text); }
3、调用JS
public void testJS(){ JavascriptExecutor executor = (JavascriptExecutor)driver; executor.executeScript("alert(‘helloworld‘)"); }
4、Wait机制及实现
public void testWait(){ WebElement waitButton = driver.findElement(By.id("wait")); waitButton.click(); boolean flag = new WebDriverWait(driver, 10).until ( new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver driver) { return driver.findElement(By.className("red")).isDisplayed(); } } ); if(flag){ String text = driver.findElement(By.className("red")).getText(); System.out.println(text); } }
5、Iframe操作
public void testIFrame(){ driver.findElement(By.id("user")).sendKeys("test"); driver.switchTo().frame("aa"); driver.findElement(By.id("user")).sendKeys("iframe test"); driver.switchTo().defaultContent();//返回顶层frame driver.findElement(By.id("user")).sendKeys("---new test"); }
6、多窗口切换
public void testMultiWindow(){ driver.findElement(By.id("user")).sendKeys("test"); String handle = driver.getWindowHandle();//获取当前窗口的句柄 System.out.println(handle); WebElement element = driver.findElement(By.className("open")); element.click(); Set<String> handles = driver.getWindowHandles(); for(String s : handles) { if(!s.equals(handle)) { System.out.println(s); driver.switchTo().window(s); driver.findElement(By.id("kw")).sendKeys("glen"); } } driver.switchTo().window(handle); driver.findElement(By.id("user")).sendKeys("---new test"); }
Selenium2(WebDriver)总结(五)---元素操作进阶(常用类)
标签:
原文地址:http://www.cnblogs.com/puresoul/p/4320684.html