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 ...
随机推荐
- ARP 扫描主机学习笔记
1.通用套接字地址结构与具体套接字地址结构之间可相互转化 1)通用转具体,某些函数将结果存储在通用套接字地址结构中,这时将通用转换为具体,具体通过访问成员名可以方便的得到结果. 2)具体转通用,为了消 ...
- 新冲刺Sprint3(第四天)
一.Sprint介绍 实现了SQLite数据库记录自动登录.注销功能 真机测试效果图: 二.Sprint周期 看板: 燃尽图:
- service的简单使用
Service的生命周期方法比Activity少一些,只有onCreate, onStart, onDestroy 我们有两种方式启动一个Service,他们对Service生命周期的影响是不一样的. ...
- C# .Net中七层架构浅析
Model实体层,DBUtility数据访问抽象类,IDAL数据访问接口层,SQLServerDAL数据访问层,DALFactory数据访问工厂类,BLL业务逻辑层,UI界面层 一.项目名称及描述:( ...
- LEETCODE —— Linked List Cycle [Floyd's cycle-finding algorithm]
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
- Http协议:彻底弄懂 Http 缓存机制 - 基于缓存策略三要素分解法
转载:http://mp.weixin.qq.com/s/uWPls0qrqJKHkHfNLmaenQ 导语 Http 缓存机制作为 web 性能优化的重要手段,对从事 Web 开发的小伙伴们来说是必 ...
- Windows下面如何建立多个Django虚拟环境
Linux 安装请见另一篇博客 http://www.cnblogs.com/zhaoyingjie/p/6180758.html 1.安装 virtualenv 2.安装virtualenvwrap ...
- php区分new static 和new self
关键点在于一个是静态绑定,一个是延迟绑定 <?php class A{ public function __construct() { } public function createObjSt ...
- Mysql 安装-操作-备份
Mysql 5.7 安装windows 1.配置mysql的path->系统属性->环境变量-path添加最后 2.配置目录和主从 [mysqld]port = 3306 basedir= ...
- mybatis与 Exception
mybatis将所有的异常全部包成了运行时异常,减少在高层代码中频繁的try-catch导致的代码臃肿问题.Persistence是它们共有的父类,继承自RuntimeException非检查型异常. ...