一、WebDriver(import org.openqa.selenium.WebDriver)操作浏览器文章源自懂站帝-http://www.sfdkj.com/13937.html
selenium通过WebDriver对象来定位页面元素文章源自懂站帝-http://www.sfdkj.com/13937.html
1、打开浏览器文章源自懂站帝-http://www.sfdkj.com/13937.html
打开火狐浏览器:文章源自懂站帝-http://www.sfdkj.com/13937.html
WebDriver driver = new FireFoxDriver();文章源自懂站帝-http://www.sfdkj.com/13937.html
打开IE浏览器:文章源自懂站帝-http://www.sfdkj.com/13937.html
WebDriver driver = new InternetExplorerDriver();文章源自懂站帝-http://www.sfdkj.com/13937.html
打开chrome浏览器文章源自懂站帝-http://www.sfdkj.com/13937.html
WebDriver driver = new ChromeDriver();文章源自懂站帝-http://www.sfdkj.com/13937.html
2、最大化浏览器文章源自懂站帝-http://www.sfdkj.com/13937.html
driver.manage().window().maximize();文章源自懂站帝-http://www.sfdkj.com/13937.html
3、关闭浏览器文章源自懂站帝-http://www.sfdkj.com/13937.html
driver.close();文章源自懂站帝-http://www.sfdkj.com/13937.html
driver.quit();文章源自懂站帝-http://www.sfdkj.com/13937.html
4、打开测试页面文章源自懂站帝-http://www.sfdkj.com/13937.html
driver.get("http:www.baidu.com");文章源自懂站帝-http://www.sfdkj.com/13937.html
二、By(org.openqa.selenium.By) 定位页面元素文章源自懂站帝-http://www.sfdkj.com/13937.html
selenium通过By对象来定位页面元素文章源自懂站帝-http://www.sfdkj.com/13937.html
1、By.id 通过ID定位页面元素文章源自懂站帝-http://www.sfdkj.com/13937.html
By.id("UserCode")文章源自懂站帝-http://www.sfdkj.com/13937.html
2、By.name 通过name定位页面元素文章源自懂站帝-http://www.sfdkj.com/13937.html
By.id("UserCode")文章源自懂站帝-http://www.sfdkj.com/13937.html
3、By.className 通过className定位页面元素文章源自懂站帝-http://www.sfdkj.com/13937.html
By.className("input_class")文章源自懂站帝-http://www.sfdkj.com/13937.html
4、By.linkText 精确查找文章源自懂站帝-http://www.sfdkj.com/13937.html
drive.get("http://www.baidu.com");文章源自懂站帝-http://www.sfdkj.com/13937.html
By.linkText("百科");文章源自懂站帝-http://www.sfdkj.com/13937.html
5、By.partialLinkText 模糊查找文章源自懂站帝-http://www.sfdkj.com/13937.html
driver.get("http://www.baidu.com");文章源自懂站帝-http://www.sfdkj.com/13937.html
By.partialLinkText("百科");文章源自懂站帝-http://www.sfdkj.com/13937.html
6、By.tagName文章源自懂站帝-http://www.sfdkj.com/13937.html
dirver.get("http://www.baidu.com");文章源自懂站帝-http://www.sfdkj.com/13937.html
By.tagName("input");文章源自懂站帝-http://www.sfdkj.com/13937.html
三、WebElement( org.openqa.selenium.WebElement)操作页面元素文章源自懂站帝-http://www.sfdkj.com/13937.html
selenium通过WebElement对象来操作页面元素文章源自懂站帝-http://www.sfdkj.com/13937.html
1、操作输入框文章源自懂站帝-http://www.sfdkj.com/13937.html
WebElement inputElement = driver.findElement(By.id("UserCode"));//查找登录页面录入用户名元素文章源自懂站帝-http://www.sfdkj.com/13937.html
inputElement.setKeys("wyl");//输入框录入用户名wyl文章源自懂站帝-http://www.sfdkj.com/13937.html
inputElement.clear();//清空输入框文章源自懂站帝-http://www.sfdkj.com/13937.html
inputElement.getText();//获得输入框中的内容文章源自懂站帝-http://www.sfdkj.com/13937.html
2、操作单选框文章源自懂站帝-http://www.sfdkj.com/13937.html
WebElement radioElement = driver.findElement(By.id("Orders"));文章源自懂站帝-http://www.sfdkj.com/13937.html
radioElement.click();// 选择某个单选项文章源自懂站帝-http://www.sfdkj.com/13937.html
radioElement.clear();// 清空某个单选项文章源自懂站帝-http://www.sfdkj.com/13937.html
radioElement.isSelected();// 判断某个单选项是否已被选择文章源自懂站帝-http://www.sfdkj.com/13937.html
3、操作多选框文章源自懂站帝-http://www.sfdkj.com/13937.html
WebElement checkboxElement = driver.findElement(By.id("Orders"));文章源自懂站帝-http://www.sfdkj.com/13937.html
checkboxElement.click();// 选择某个多选项文章源自懂站帝-http://www.sfdkj.com/13937.html
checkboxElement.clear();// 清空某个多选项文章源自懂站帝-http://www.sfdkj.com/13937.html
checkboxElement.isSelected();//判断某个多选项是否已被选择文章源自懂站帝-http://www.sfdkj.com/13937.html
4、操作下拉框文章源自懂站帝-http://www.sfdkj.com/13937.html
Select select = new Select(diver.findElement(By.id("region")));文章源自懂站帝-http://www.sfdkj.com/13937.html
select.selectByVisibleText("北京市");文章源自懂站帝-http://www.sfdkj.com/13937.html
select.selectByText("10010");文章源自懂站帝-http://www.sfdkj.com/13937.html
select.deselectAll();文章源自懂站帝-http://www.sfdkj.com/13937.html
select.deselectByVisibleText("北京市");文章源自懂站帝-http://www.sfdkj.com/13937.html
select.deselectByText("10010");文章源自懂站帝-http://www.sfdkj.com/13937.html
select.getAllSelectedOptions();文章源自懂站帝-http://www.sfdkj.com/13937.html
select.getFirstSelectedOption();文章源自懂站帝-http://www.sfdkj.com/13937.html
5、操作上传文件文章源自懂站帝-http://www.sfdkj.com/13937.html
WebElement uploadElement = driver.findElement(By.id("file"));文章源自懂站帝-http://www.sfdkj.com/13937.html
String uploadFile = "D:\\1\\AgentCode.txt";文章源自懂站帝-http://www.sfdkj.com/13937.html
uploadElement.setKeys(uploadFile);文章源自懂站帝-http://www.sfdkj.com/13937.html
6、操作按钮文章源自懂站帝-http://www.sfdkj.com/13937.html
WebElement btnElement = driver.findElement(By.id("add"));文章源自懂站帝-http://www.sfdkj.com/13937.html
btnElement.click();文章源自懂站帝-http://www.sfdkj.com/13937.html
btnElement.isEnabled();文章源自懂站帝-http://www.sfdkj.com/13937.html
7、表单提交文章源自懂站帝-http://www.sfdkj.com/13937.html
WebElement formElement = driver.findElement(By.id("fm"));文章源自懂站帝-http://www.sfdkj.com/13937.html
fromElement.submit();文章源自懂站帝-http://www.sfdkj.com/13937.html
8、操作弹出对话框文章源自懂站帝-http://www.sfdkj.com/13937.html
Alert alert = driver.swichTo().alert();文章源自懂站帝-http://www.sfdkj.com/13937.html
alert.getText();//获得弹出框内容文章源自懂站帝-http://www.sfdkj.com/13937.html
alert.accept();//点击弹出框确认按钮文章源自懂站帝-http://www.sfdkj.com/13937.html
alert.dismiss();//点击弹出框取消按钮文章源自懂站帝-http://www.sfdkj.com/13937.html
9、Windows和Frames之间的切换文章源自懂站帝-http://www.sfdkj.com/13937.html
driver.switchTo().defaultContent();//返回到最顶层的frame文章源自懂站帝-http://www.sfdkj.com/13937.html
driver.switchTo().frame("leftMenuFrame");//切换到左侧菜单frame文章源自懂站帝-http://www.sfdkj.com/13937.html
driver.switchTo().window("windowName");//切换到某个windows文章源自懂站帝-http://www.sfdkj.com/13937.html
10、超时设置文章源自懂站帝-http://www.sfdkj.com/13937.html
driver.manage().timeouts().implicitly(10,timeUnit.Seconds);//识别元素时的超时时间文章源自懂站帝-http://www.sfdkj.com/13937.html
driver.manage().timeouts().pageLoadTimeout(10,timeUnit.Seconds);//页面加载时的超时时间文章源自懂站帝-http://www.sfdkj.com/13937.html
driver.manage().timeouts().setScriptTimeout(10,timeUnit.Seconds);//异步脚本的超时时间文章源自懂站帝-http://www.sfdkj.com/13937.html
四、调用js文章源自懂站帝-http://www.sfdkj.com/13937.html
JavascriptExecutor js = (JavascriptExecutor)driver;文章源自懂站帝-http://www.sfdkj.com/13937.html
js.executeScript("JS脚本");文章源自懂站帝-http://www.sfdkj.com/13937.html
五、Selenium官方文章源自懂站帝-http://www.sfdkj.com/13937.html
1、官方download包下载地址文章源自懂站帝-http://www.sfdkj.com/13937.html
http://code.google.com/p/selenium/downloads/list文章源自懂站帝-http://www.sfdkj.com/13937.html
2、官方User Guide文章源自懂站帝-http://www.sfdkj.com/13937.html
http://seleniumhq.org/docs/文章源自懂站帝-http://www.sfdkj.com/13937.html
3、API文章源自懂站帝-http://www.sfdkj.com/13937.html
http://selenium.googlecode.com/git/docs/api/java/index.html文章源自懂站帝-http://www.sfdkj.com/13937.html