自学selenium第五节-selenium各种类的使用
2015-06-07 18:18
597 查看
package selenium.test; import java.util.List; import java.util.Set; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.Select; import org.openqa.selenium.support.ui.WebDriverWait; public class class5 { public WebDriver driver; public void startFirefox() { driver =new FirefoxDriver(); driver.manage().window().maximize(); } public void startChrome() { System.setProperty("webdriver.chrome.driver", "files/chromedriver.exe"); driver=new ChromeDriver(); driver.manage().window().maximize(); } public void closeChrome() { driver.close(); driver.quit(); } public void goTo(String url) { //System.out.println(url); driver.navigate().to(url); } public void test_alert(String xpath) { WebElement element=driver.findElement(By.xpath(xpath)); Actions action=new Actions(driver); action.click(element).perform();//action类的使用 Alert alert=driver.switchTo().alert(); String text=alert.getText(); System.out.println(text); alert.accept(); //alert.dismiss();取消操作 } public void testAction(String xpath) { WebElement element=driver.findElement(By.xpath(xpath)); Actions action=new Actions(driver); String actionText = driver.findElement(By.id("over")).getText();//还没有进行JS注入,div[@id='over']中值为空 System.out.println(actionText); action.moveToElement(element).perform(); actionText = driver.findElement(By.id("over")).getText(); System.out.println(actionText); } public void upload(String xpath,String filename) { WebElement element=driver.findElement(By.xpath(xpath)); element.sendKeys(filename);//把需要上传的文件赋值到上床到框体 } public void testjs() { //driver.get("http://www.haosou.com/"); //String ret=(String) ((JavascriptExecutor)driver).executeScript("return document:getElementById('search-button').value;"); //System.out.println(ret); JavascriptExecutor j=(JavascriptExecutor)driver; j.executeScript("alert('鹁鸽萨比')"); //System.out.println("1111"); } public void testIframe() { driver.findElement(By.id("user")).sendKeys("Tang");//页面原框体 driver.switchTo().frame("aa"); driver.findElement(By.id("user")).sendKeys("MaXiaochu");//frame内框体 driver.switchTo().defaultContent();//切换回原页面 try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } driver.findElement(By.id("user")).clear(); driver.findElement(By.id("user")).sendKeys("TangKuanpeng");//页面原框体 } public void testSwitchWindows() { WebElement element =driver.findElement(By.xpath("//a[@class='open']")); element.click();//激活页面 Set <String> handles=driver.getWindowHandles();//得到的是所有页面的句柄列表 String handle =driver.getWindowHandle();//得到的是最初默认窗体的句柄 handles.remove(handle);//不执行这一句,handle和newhandle回相同 String newhandle=handles.iterator().next(); //driver.findElement(By.id("kw")).sendKeys("TangKuanpeng"); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } driver.switchTo().window(handle);//切回原窗口 try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } driver.switchTo().window(newhandle);//切回新窗体 driver.close(); //System.out.println(handle); //System.out.println(newhandle);//拿到的newhandle和handle是一个值 } public void testWait() { WebElement element=driver.findElement(By.className("wait")); element.click(); boolean wait=new WebDriverWait(driver,10).until ( new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver driver){ return driver.findElement(By.className("red")).isDisplayed(); } } ); System.out.println(wait); element=driver.findElement(By.className("red")); String wait_text=element.getText(); System.out.println(wait_text); } public static void main(String[] args) { class5 t=new class5(); //t.startChrome(); //t.goTo("http://www.baidu.com"); t.startFirefox(); t.goTo("file:///D:/demo_new/demo_new.html"); t.test_alert("//input[@class='alert']"); t.testAction("//input[@value='Action']"); t.upload("//input[@id='load']","D:\\test.txt"); //t.testjs(); //t.testIframe(); //t.testSwitchWindows(); t.testWait(); } }
相关文章推荐
- CCNA学习指南 无线技术
- XMPPclient库Smack 4.0.6一个版本的开发
- c++ --> 复制构造函数
- 数据库 数据库诊断信息
- Digit (数位DP)
- 乘法口诀 (第一次写博客~~~~)
- 数学之美札记:信息的度量和作用
- Rotate List || LeetCode
- c语言编写贪吃蛇修改后最新版
- 如何让非root用户启用小于1024号的端口
- 投稿
- Redis常用命令
- UVA 12297 Super Poker(矩阵快速幂)
- HTTP POST GET 本质区别详解
- Uva 1584 Circular Sequence
- 将截断字符串或二进制数据。语句已终止
- SSH三种框架及表示层、业务层和持久层的理解
- <深入理解JavaScript>学习笔记(5)_强大的原型和原型链
- Android 仿美团网,大众点评购买框悬浮效果之修改版
- LightOJ1012---Guilty Prince (并查集)