selenium2.0(WebDriver) API
1.1 下载selenium2.0的包
- 官方download包地址:http://code.google.com/p/selenium/downloads/list
- 官方User Guide: http://seleniumhq.org/docs/
- 官方API: http://selenium.googlecode.com/git/docs/api/java/index.html
1.2.1 用webdriver打开一个浏览器
- 打开firefox浏览器:
WebDriver driver = new FirefoxDriver();
- 打开IE浏览器
WebDriver driver = new InternetExplorerDriver ();
- 打开HtmlUnit浏览器
WebDriverdriver = new HtmlUnitDriver();
- 打开chrome浏览器
WebDriverdriver = new ChromeDriver();
1.2.2 最大化浏览器
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
1.2.3 关闭浏览器
WebDriver driver = new FirefoxDriver();
- driver.close();
- driver.quit();
1.3 打开测试页面
- driver.get("http://www.google.com");
- driver.navigate().to("http://www.baidu.com/");
P.S.navigate方法会产生1个Navigator对象,其封装了与导航相关的一些方法,比如前进后退等
1.4 页面元素定位
Webdriver提供下面两种方法来定位页面元素,参数是By对像,最常用是By.id和By.name查找。
- findElement 定位某个元素,如果没有找到元素会抛出异常:NoSuchElementException
- findElements 定位一组元素
例如需要定位如下元素:
<input class="input_class" type="text" name="passwd" id="passwd-id" />
- By.id:
WebElement element = driver.findElement(By.id("passwd-id"));
- By.name:
WebElement element = driver.findElement(By.name("passwd"));
- By.xpath:
WebElement element =driver.findElement(By.xpath("//input[@id='passwd-id']"));
- By.className
WebElement element = driver.findElement(By.className("input_class"));
- By.cssSelector
WebElement element = driver.findElement(By.cssSelector(".input_class"));
- By.linkText:
//通俗点就是精确查询
WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com/");
WebElement element = driver.findElement(By.linkText("百科"));
- By.partialLinkText:
//这个方法就是模糊查询
WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com/");
WebElement element = driver.findElement(By.partialLinkText("hao"));
- By.tagName:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com/");
String test= driver.findElement(By.tagName("form")).getAttribute("name");
System.out.println(test);
1.5 如何对页面元素进行操作
1.5.1 输入框(text field or textarea)
WebElement element = driver.findElement(By.id("passwd-id"));
- element.sendKeys(“test”);//在输入框中输入内容:
- element.clear(); //将输入框清空
- element.getText(); //获取输入框的文本内容:
1.5.2下拉选择框(Select)
Select select = new Select(driver.findElement(By.id("select")));
- select.selectByVisibleText(“A”);
- select.selectByValue(“1”);
- select.deselectAll();
- select.deselectByValue(“1”);
- select.deselectByVisibleText(“A”);
- select.getAllSelectedOptions();
- select.getFirstSelectedOption();
1.5.3单选项(Radio Button)
WebElement radio=driver.findElement(By.id("BookMode"));
- radio.click(); //选择某个单选项
- radio.clear(); //清空某个单选项
- radio.isSelected(); //判断某个单选项是否已经被选择
1.5.4多选项(checkbox)
WebElement checkbox = driver.findElement(By.id("myCheckbox."));
- checkbox.click();
- checkbox.clear();
- checkbox.isSelected();
- checkbox.isEnabled();
1.5.5按钮(button)
WebElement btn= driver.findElement(By.id("save"));
- btn.click(); //点击按钮
- btn.isEnabled (); //判断按钮是否enable
1.5.7弹出对话框(Popup dialogs)
Alert alert = driver.switchTo().alert();
- alert.accept(); //确定
- alert.dismiss(); //取消
- alert.getText(); //获取文本
1.5.8表单(Form)
Form中的元素的操作和其它的元素操作一样,对元素操作完成后对表单的提交可以:
WebElement approve = driver.findElement(By.id("approve"));
approve.click();
或
approve.submit();//只适合于表单的提交
1.5.9上传文件
上传文件的元素操作:
WebElement adFileUpload =driver.findElement(By.id("WAP-upload"));
String filePath = "C:\test\\uploadfile\\media_ads\\test.jpg";
adFileUpload.sendKeys(filePath);
1.6 Windows 和 Frames之间的切换
- driver.switchTo().defaultContent(); //返回到最顶层的frame/iframe
- driver.switchTo().frame("leftFrame"); //切换到某个frame:
- driver.switchTo().window("windowName"); //切换到某个window
1.7 调用Java Script
Web driver对Java Script的调用是通过JavascriptExecutor来实现的,例如:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("JS脚本");
1.8 超时设置
WebDriver driver = new FirefoxDriver();
- driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //识别元素时的超时时间
- driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS); //页面加载时的超时时间
- driver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS); //异步脚本的超时时间
selenium2.0(WebDriver) API的更多相关文章
- Selenium2.0 Webdriver 随笔
Webdriver can't action the element when the element is out of view 1. Scroll to the element use Java ...
- Selenium 2.0 WebDriver 自动化测试 使用教程 实例教程 API快速参考
Selenium 2.0 WebDriver 自动化测试 使用教程 实例教程 API快速参考 //System.setProperty("webdriver.firefox.bin" ...
- 在selenium2.0中使用selenium1.0的API
Selenium2.0中使用WeDriver API对页面进行操作,它最大的优点是不需要安装一个selenium server就可以运行,但是对页面进行操作不如selenium1.0的Selenium ...
- selenium2(WebDriver) API
selenium2(WebDriver) API 作者:Glen.He出处:http://www.cnblogs.com/puresoul/ 1.1 下载selenium2.0的包 官方downl ...
- Selenium2+Python:Webdriver API速记手册
由于web自动化常常需要控制浏览器行为和操作页面元素,相关函数又比较多,于是再此记下一份Webdriver API查阅文档以备不时之需. 参考:虫师<Selenium2自动化测试实战>,和 ...
- Python版:Selenium2.0之WebDriver学习总结_实例1
Python版:Selenium2.0之WebDriver学习总结_实例1 快来加入群[python爬虫交流群](群号570070796),发现精彩内容. 实属转载:本人看的原文地址 :http:/ ...
- Webdriver API (二)
(转载) 1.3 打开测试页面 对页面对测试,首先要打开被测试页面的地址(如:http://www.google.com),web driver 提供的get方法可以打开一个页面: // And no ...
- Webdriver API (一)
(转载) 1.1 下载selenium2.0的包 官方download包地址:http://code.google.com/p/selenium/downloads/list 官方User Guid ...
- selenium2.0处理case实例(二)
本文通过具体代码处理过程, 来展示selenium中一些比较不常用的类的用法 1.javascriptExcutor,通过将driver强转成JavascriptExecutor类型, 调用execu ...
随机推荐
- 【转】使用Eclipse搭建Python开发环境
因为要进行自动化测试,所以要搭建Python开发环境.这里将使用Eclipse+pyDev进行搭建,在此作为笔记记录下来. 需要的组件: 1.Eclipse SDK 3.7(这里将不再叙述Eclips ...
- windows系统下npm 全局安装路径问题
安装了nodejs之后,npm的路径默认一直都是appData,表示很讨厌,于是尝试修改在安装目录(D盘空间很大啊) 安装目录:D:\program files\nodejs 一.在nodejs下新建 ...
- 关于判断checkbox选中问题
attr和prop的区别: 因为DOM节点属性可以理解为静态的,当页面渲染完,checked属性就确定了,就是checked.而HTML元素属性是动态的,随时可以改变,而且对于checked这个属性, ...
- null、undefined、false、0相等性比较
之前在看<JavaScript权威指南>的时候看到三个相等性比较的式子: null == undefined ;// ==>true undefined == false;// == ...
- 数论 UVA 10791
这道题目是关于满足同意最小公倍数的所有数对中两数之和的最小值. 题目大意是给你一个数n,要求你求出在所有以n为最小公倍数的数对中两数之和的最小值. 方法:将n进行质因数分解,再将所有分解出的质因子加起 ...
- ajax处理的方式
1.方式一:在一个web页面中 [System.Web.Services.WebMethod],有点象webservers的方法! 前台: 返回的数据: msg:Object {d: "He ...
- pthread_cond_wait()函数的理解(摘录)
/************pthread_cond_wait()的使用方法**********/pthread_mutex_lock(&qlock); /*lock*/pthread_c ...
- CentOS上yum安装JDK
转: http://blog.csdn.net/onepiecehuiyu/article/details/17189571
- Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 用word制作电子书最简捷模式 支持epub和mobi目录
因为制作一本OCR的电子书,转到word编辑排版后,用calibre转成mobi发现没有目录,在网上查了资料研究了一下,终于解决了目录问题,根本不用将word文档转换为什么htm或txt,尤其是转换t ...